home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / AppleScript / Development Tools / Sample Code / 7Edit / Pascal Sources / SVAppleEvents.p < prev    next >
Encoding:
Text File  |  1994-02-25  |  166.0 KB  |  5,995 lines  |  [TEXT/MPS ]

  1. {[d-,h-,k+,o=100,q+,r+,rec+,t=2,u+,:+,j=15/20/25/30/35/40/45/50/57/1$]} {Pasmat opts!}
  2.  
  3. UNIT SVAppleEvents;
  4.  
  5. (*
  6.     SVAppleEvents.p 
  7.   
  8.     Version 3.0d8
  9.     
  10.     Copyright © SRL Data 1992, 1993
  11.     
  12.     All rights reserved
  13.     
  14.     Produced by : SRL Data
  15.     Originally Developed for UK.DTS
  16.  
  17. *)
  18.  
  19. { New for 3.0d1 :
  20.         Recommenting of many functions to improve clarity.
  21.         Implementation of nearly all menu commands via Apple Events.
  22.         Page Setup on each window (with visible page breaks)
  23.         Show Borders on each window with bug fixes in display.
  24.         
  25.     New for 3.0d2 :
  26.         19-Feb-92 : NH : Implement printing
  27.                      Fix drawing on scrollbar on show page breaks
  28.                                          Make IssueAEOpenDoc a function (for printing)
  29.                                          Make Paste Styled
  30.         
  31.         21-Feb-92 : NH : Use new (in SVEditions.p) calls for updating section info.
  32.         
  33.         27-Feb-92 : NH : SendTextMessage zapped and other routines used solely by it.
  34.         
  35.         
  36.          5-Mar-92 : NH : Added HandleCreatePub, IssueCreatePub
  37.         18-Mar-92 : NH : Zap PoseMessageDialog & mod AdornDefault
  38.         27-Mar-92 : NH : Call AssocAllSections when saved
  39.         
  40.         Start of updating to constants in Winter AERegistry.
  41.         
  42.          3-Jun-92 : NH : Check for AEInteraction allowed before printing.
  43.          
  44.   New for 3.0d3 :
  45.     
  46.         16-Jun-92 : NH : Continue updating to match latest registry.
  47.                          typeText gone - now typeChar
  48.                                          classes of kAECut etc. changed (see registry)
  49.                                          Added pIsModified code.
  50.         17-Jun-92 : NH : Modify HandleSetData - Use AEResolve to yield tokenDescriptor, then
  51.                          extract appropriate token data.
  52.         18-Jun-92 : NH : Tidy up
  53.                                          Fix AEDescs being left after send of events
  54.                          Ensure SendAESetObjProp zaps all supplied params - now VAR params
  55.                                          Zap AEDescs left behind in HandleGetData.
  56.         26-Jun-92 : NH : Zap AddNamedStyleItem, AddNamedStyleItem, BuildStyleDescList
  57.                          Add code for typeTextStyles as per registry.
  58.                                          DoOpenDocument fixed to cope with no files in list etc.
  59.                                          Fixed buggy cast of windowToken to DPtr in DoCloseWindow
  60.                                          HandleShowSelection modified to take textTokens and WindowTokens
  61.                                          Fix sizeof in MakeSelfAddress.
  62.         25-Jun-92 : NH : StyleToName zapped
  63.         
  64.     Changes for 3.0d4 :
  65.     
  66.        3-Jul-92 : NH : Fix SetWindowProperty typeMyText
  67.          7-Jul-92 : NH : Add missing window props - pIsFloating, pHasCloseBox, pIsZoomable
  68.         13-Jul-92 : NH : Complete the menu classes.
  69.                          Allow -ve absolute positions - i.e. last line of ...
  70.                                          Change Line access to allow relative offsets
  71.       14-Jul-92 : NH : Change region massages for window positions (thanks huggy!)
  72.         15-Jul-92 : NH : Modified HandleGetData/SetData to allow styledText and typeChar
  73.                          Recording of keyStrokes
  74.         17-Jul-92 : NH : Enter key -> compile text - filtered out too
  75.                          Fix setting of styles so that styles not in on/off styles untouched
  76.          5-Aug-92 : NH : Add pUserSelection
  77.                                           Zap cSelection
  78.                                          Added MakeSelectedTextObject for Gustav
  79.                                          Styles made to match behaviour in Inside Mac VI
  80.                                          Fixed setting of styles to match registry
  81.                                          Fixed saving to work after name change of window
  82.                                          Mark document as dirty after SetData
  83.                                          Feed back all filing errors
  84.                                          Moved some data from descriptor procedures to SVEditAEUtils.c
  85.                                          
  86.     Changes for 3.0d5: 
  87.     
  88.         10-Aug-92 : NH : Fixed Quit save no - AEInteractWithUser called now
  89.                                          Added IssueQuitCommand.
  90.                                          Install cLine from typeMyText accessor
  91.     Changes for 3.0d6: 
  92.     
  93.         10-Sep-92 : JL : Fixed gTypingBuffer- this was not initialised in the Pascal
  94.                          version, although it was in C.
  95.                                          Added check for gRecordingImplemented when sending text just for
  96.                                          recording.
  97.     Changes for 3.0d7: 
  98.     
  99.         16-Nov-92 : JL : Changed pUserSelection to pSelection
  100. }
  101.  
  102. {
  103. This file includes :
  104.  
  105.     a) the code for the AppleEvent initialisation 
  106.     b) the routines for all the Edition Manager events
  107.     c) the Apple® Event Object Support
  108. }
  109.  
  110. INTERFACE
  111.  
  112. USES
  113.         MemTypes, 
  114.         QuickDraw,
  115.         Packages,
  116.         GestaltEqu,
  117.         
  118.         AppleEvents,
  119.         AEObjects,
  120.         AEPackObject,
  121.         Editions, 
  122.         Printing,
  123.         Menus,
  124.         
  125.         SVEditGlobals,
  126.         SVEditUtils,
  127.         SVEditFile, 
  128.         SVEditWindow, 
  129.         SVEditions;
  130.  
  131. PROCEDURE InitAppleEvents;
  132. PROCEDURE DoAppleEvent(theEvent : EventRecord);
  133.  
  134. (*
  135.     Text Commands
  136. *)
  137. PROCEDURE IssueCutCommand(theDocument:DPtr);
  138. PROCEDURE IssueCopyCommand(theDocument:DPtr);
  139. PROCEDURE IssuePasteCommand(theDocument:DPtr);
  140. PROCEDURE IssueClearCommand(theDocument:DPtr);
  141. PROCEDURE IssueFontCommand(theDocument:DPtr;theItem:INTEGER);
  142. PROCEDURE IssueSizeCommand(theDocument:DPtr;theItem:INTEGER);
  143. PROCEDURE IssueStyleCommand(theDocument:DPtr;theItem:INTEGER);
  144.  
  145. (*
  146.     Window Commands
  147. *)
  148.  
  149. PROCEDURE IssueZoomCommand(whichWindow:WindowPtr; whichPart:INTEGER);
  150. PROCEDURE IssueCloseCommand(whichWindow:WindowPtr);
  151. PROCEDURE IssueSizeWindow(whichWindow:WindowPtr; newHSize:INTEGER; newVSize:INTEGER);
  152. PROCEDURE IssueMoveWindow(whichWindow:windowPtr; sizeRect:Rect);
  153. PROCEDURE IssuePageSetupWindow(whichWindow:windowPtr; thePageSetup:TPrint);
  154. PROCEDURE IssueShowBorders(whichWindow:windowPtr; showBorders:BOOLEAN);
  155. PROCEDURE IssuePrintWindow(whichWindow:windowPtr);
  156.  
  157. (*
  158.     Document Commands
  159. *)
  160.  
  161. FUNCTION  IssueAEOpenDoc(myFSSpec: FSSpec):OSErr;
  162. PROCEDURE IssueAENewWindow;
  163. FUNCTION  IssueSaveCommand(theWindow    : WindowPtr;
  164.                                                 where            : FSSpecPtr):OSErr;
  165.                                                 
  166. FUNCTION  IssueRevertCommand(theWindow    : WindowPtr):OSErr;
  167. FUNCTION  IssueQuitCommand:OSErr;
  168.  
  169. PROCEDURE IssueCreatePublisher(whichDoc:DPtr);
  170.  
  171. (*
  172.     Recording of Keystrokes
  173. *)
  174.  
  175. PROCEDURE AddKeyToTypingBuffer(theDocument : DPtr; theKey :char);
  176.  
  177. PROCEDURE FlushAndRecordTypingBuffer;
  178.  
  179. IMPLEMENTATION
  180.  
  181. USES
  182.     Fonts,
  183.     Scrap,
  184.     SysEqu,
  185.     AERegistry,
  186.     SVEditAEUtils;
  187.     
  188. CONST     
  189.         
  190.         { these should come from the registry }
  191.         
  192.         kAEStartedRecording     = 'rec1';
  193.         kAEStoppedRecording     = 'rec0';
  194.         
  195.                 
  196.         pText                     =     'TEXT';
  197.         cSpot                     =     'cspt';
  198.                                                 
  199.         (*
  200.             Text Properties
  201.         *)
  202.         
  203.         pStringWidth                        =   'pwid';
  204.         
  205.         (*
  206.             Window Properties - See the Registry for Details
  207.         *)
  208.  
  209.         pPosition                                =    'ppos';
  210.  
  211.         pPageSetup                            = 'PSET'; (* One of ours - Not in registry *)
  212.         pShowBorders                        = 'PBOR'; (* Another of ours *)
  213.                 
  214.         typeTPrint                            = 'TPNT'; (* A raw TPrint record - also one of ours *)
  215.                                 
  216.     (*
  217.             Error Codes
  218.         *)
  219.         
  220.         kAEGenericErr           = -1799;
  221.  
  222. TYPE typingBuffer = PACKED ARRAY[0..31999] OF CHAR;
  223.      typingBufPtr = ^typingBuffer;
  224.  
  225. VAR gBigBrother         : INTEGER; (* Count of Scripting Systems recording us *)
  226.     gTypingBuffer       : typingBufPtr;
  227.     gCharsInBuffer      : INTEGER;
  228.     gTypingTargetObject : AEDesc;
  229.         
  230. {-----------------------------------------------------------------------}
  231. {*----------                         APPLE EVENT HANDLING                     ---------------*}
  232. {-----------------------------------------------------------------------}
  233.  
  234. {$S Main}
  235.  
  236. {*-----------------------------------------------------------------------
  237.     Utility Routines for getting data from AEDesc's
  238.   -----------------------------------------------------------------------*}
  239.  
  240. FUNCTION GetTHPrintFromDescriptor(sourceDesc   : AEDesc;
  241.                                                                 VAR result   : THPrint):OSErr;
  242. VAR myErr           : OSErr;
  243.     ignoreErr   : OSErr;
  244.         ptSize          : Size;
  245.         resultDesc    : AEDesc;
  246.         
  247. BEGIN    
  248.     myErr := AECoerceDesc(sourceDesc,typeTPrint,resultDesc);
  249.     
  250.     result := NIL;
  251.     
  252.     IF (myErr=noErr) THEN
  253.         BEGIN
  254.             result := THPrint(NewHandle(SizeOf(TPrint)));
  255.             
  256.             PrOpen;
  257.             PrintDefault(result);
  258.             
  259.             HLock(Handle(result));
  260.  
  261.             GetRawDataFromDescriptor(resultDesc,
  262.                                                              @result^^,
  263.                                                              sizeof(TPrint),
  264.                                                              ptSize);
  265.                                                              
  266.             HUnLock(Handle(result));
  267.             
  268.             IF (ptSize<sizeof(TPrint)) OR
  269.                  (PrValidate(result)) THEN
  270.                 BEGIN
  271.                     myErr := errAECoercionFail;
  272.                     DisposHandle(Handle(result));
  273.                     result := NIL;
  274.                 END;
  275.             
  276.             PrClose;
  277.         END;
  278.     
  279.     IF (resultDesc.dataHandle<>NIL) THEN
  280.         ignoreErr := AEDisposeDesc(resultDesc);
  281.         
  282.     GetTHPrintFromDescriptor := myErr;
  283. END; (*GetTHPrintFromDescriptor*)
  284.  
  285.  
  286. {*****************************************************************************}
  287. {
  288.     Object Accessors - Utility Routines
  289. }
  290.  
  291. {$S ObjectAccessors}
  292.  
  293. FUNCTION WindowNameToWindowPtr(nameStr: StringPtr): WindowPtr;
  294.     Returns the WindowPtr of the window with title nameStr
  295.     or NIL if there is no matching window.
  296. }
  297. VAR theWindow :    WindowPtr;
  298.       windTitle :    Str255;
  299.         
  300. BEGIN
  301.   WindowNameToWindowPtr := NIL;
  302.   theWindow := WindowPtr(Handle(WindowList)^);
  303.   { 
  304.         iterate through windows - we use WindowList 'cos we could
  305.         have made the window invisible and then we lose it - so we
  306.         can't set it back to visible!!
  307.     }
  308.   WHILE theWindow <> NIL DO
  309.     BEGIN
  310.             GetWTitle(theWindow,windTitle);
  311.             IF EqualString(windTitle,nameStr^,FALSE,TRUE) THEN    { ignore case, don't ignore diacriticals }
  312.                 BEGIN
  313.                     WindowNameToWindowPtr := theWindow;
  314.                     EXIT(WindowNameToWindowPtr);
  315.                 END;
  316.       theWindow := WindowPtr(WindowPeek(theWindow)^.nextWindow);
  317.     END;
  318. END;    { WindowNameToWindowPtr }
  319.  
  320. FUNCTION GetWindowPtrOfNthWindow(index: INTEGER): WindowPtr;
  321. { returns a ptr to the window with the given index
  322.   (front window is 1, behind that is 2, etc.).  If
  323.   there's no window with that index (inc. no windows
  324.   at all), returns NIL.
  325. }
  326. VAR theWindow    :    WindowPtr;
  327.         
  328. BEGIN
  329.   GetWindowPtrOfNthWindow := NIL;
  330.   theWindow := WindowPtr(Handle(WindowList)^);
  331.  
  332.   { iterate through windows }
  333.     
  334.   WHILE (theWindow <> NIL) DO
  335.       BEGIN
  336.         index := index-1;
  337.             IF (index <= 0) THEN
  338.                 BEGIN
  339.                     GetWindowPtrOfNthWindow := theWindow;
  340.                     EXIT(GetWindowPtrOfNthWindow);
  341.                 END;
  342.                 
  343.       theWindow := WindowPtr(WindowPeek(theWindow)^.nextWindow);
  344.         END;
  345. END;    { GetWindowPtrOfNthWindow }
  346.  
  347. FUNCTION CountWindows:INTEGER;
  348.     VAR theWindow : WindowPtr;
  349.         index     : INTEGER;
  350.             
  351.     BEGIN
  352.         index := 0;
  353.         theWindow := WindowPtr(Handle(WindowList)^);
  354.     
  355.         { iterate through windows }
  356.         
  357.         WHILE (theWindow <> NIL) DO
  358.             BEGIN
  359.                 index := index+1;                    
  360.                 theWindow := WindowPtr(WindowPeek(theWindow)^.nextWindow);
  361.             END;
  362.         
  363.         CountWindows := index;
  364.         
  365.     END; (*CountWindows*)
  366.     
  367. FUNCTION ItemForNamedFont(theName:Str255):INTEGER;
  368.     VAR itemName : Str255;
  369.             limit    : INTEGER;
  370.     
  371.     BEGIN
  372.         ItemForNamedFont := 0;
  373.         limit := CountMItems(myMenus[fontM]);
  374.         WHILE (limit>0) DO
  375.             BEGIN
  376.                 GetItem(myMenus[fontM],limit,itemName);
  377.                 IF IUEqualString(theName, itemName)=0 THEN
  378.                     BEGIN
  379.                         ItemForNamedFont := limit;
  380.                         limit:=0;
  381.                     END
  382.                 ELSE
  383.                     limit := limit-1;
  384.             END;
  385.     END; (*ItemForNamedFont*)
  386.  
  387. {*-----------------------------------------------------------------------
  388.         Name:             DoOpenApp
  389.         Purpose:        Called on startup, creates a new document.
  390.     -----------------------------------------------------------------------*}
  391.  
  392. {$S Main}
  393. FUNCTION DoOpenApp( message, reply: AppleEvent; refcon: LONGINT ): OSErr;             
  394. VAR ourDoc : DPtr;
  395.  
  396. BEGIN
  397.     {just create a new document}
  398.     ourDoc := NewDocument(false);
  399.     
  400.     IF ourDoc<>NIL THEN
  401.         BEGIN
  402.             ShowWindow(ourDoc^.theWindow);
  403.             DoOpenApp := noErr
  404.         END
  405.     ELSE
  406.         DoOpenApp := -108;
  407. END;
  408.  
  409. {*-----------------------------------------------------------------------
  410.         Name:             DoOpenDocument
  411.         Purpose:        Open all the documents passed in the Open AppleEvent.
  412. -----------------------------------------------------------------------*}
  413.  
  414. {$S Main}
  415. FUNCTION DoOpenDocument( message, reply: AppleEvent; refcon: LONGINT ): OSErr;            
  416. VAR
  417.         index,
  418.         itemsInList : LONGINT;
  419.         keywd             : AEKeyWord;
  420.         err                 : OSErr;
  421.         ignoreErr     : OSErr;
  422.         docList         : AEDescList;
  423.         actSize         : LONGINT;
  424.         typeCode        : DescType;
  425.         theFSSpec     : FSSpec;
  426.         
  427.  
  428. BEGIN
  429.     {open the specified documents}
  430.   
  431.     docList.dataHandle := NIL;
  432.     
  433.     err := AEGetParamDesc(message, keyDirectObject, typeAEList, docList) ;
  434.     IF (err=noErr) THEN
  435.         err := AECountItems( docList, itemsInList)
  436.     ELSE
  437.         itemsInList := 0;
  438.         
  439.     FOR index := 1 TO itemsInList DO
  440.         IF (err=noErr) THEN
  441.             BEGIN
  442.                 err := AEGetNthPtr( docList,
  443.                                     index,
  444.                                                         typeFSS, 
  445.                                                         keywd, 
  446.                                                         typeCode,
  447.                                               @theFSSpec,
  448.                                                         sizeof(theFSSpec),
  449.                                                         actSize);
  450.                 IF (err=noErr) THEN
  451.                  err := OpenOld(theFSSpec);
  452.             END;
  453.             
  454.     IF (docList.dataHandle<>NIL) THEN
  455.         ignoreErr := AEDisposeDesc(docList);
  456.         
  457.     DoOpenDocument := err;
  458. END;
  459.  
  460. {*-----------------------------------------------------------------------
  461.         Name:             MyQuit
  462.         Purpose:        Quit event received- exit the program.
  463.     -----------------------------------------------------------------------*}
  464.  
  465. {$S Main}
  466. FUNCTION MyQuit(message : AppleEvent;reply : AppleEvent;refcon:LONGINT): OSErr;
  467.     VAR
  468.         saveOpt      : DescType;
  469.         tempErr      : OSErr;
  470.         myErr        : OSErr;
  471.         returnedType : DescType;
  472.         actSize      : LONGINT;
  473.         
  474.     BEGIN
  475.         saveOpt := kAEAsk; (* the default *)
  476.         tempErr := AEGetParamPtr(message,
  477.                                                          keyAESaveOptions,
  478.                                                          typeEnumerated,
  479.                                                          returnedType,
  480.                                                          Ptr(@saveOpt),
  481.                                                          sizeof(saveOpt),
  482.                                                          actSize);
  483.         
  484.         IF (saveOpt <> kAENo) THEN
  485.             myErr := AEInteractWithUser(kAEDefaultTimeout, nil, nil);
  486.  
  487.       IF (myErr = noErr) THEN
  488.             DoQuit(saveOpt);
  489.         
  490.         myQuit := myErr;
  491.     END;
  492.  
  493. {*-----------------------------------------------------------------------
  494.         Name:             DoAppleEvent
  495.         Purpose:        Process and despatch the AppleEvent
  496.     -----------------------------------------------------------------------*}
  497.  
  498. {$S Main}
  499. PROCEDURE DoAppleEvent(theEvent : EventRecord);
  500.  
  501. VAR err: OsErr;
  502.  
  503. BEGIN
  504.     {should check for your own event message types here - if you have any}
  505.     
  506.     err := AEProcessAppleEvent(theEvent);
  507. END;
  508.  
  509. {*-----------------------------------------------------------------------
  510.         Name:             MakeSelfAddress
  511.         Purpose:        Builds an AEAddressDesc for the current process
  512.     -----------------------------------------------------------------------*}
  513.     
  514. FUNCTION MakeSelfAddress(VAR selfAddress : AEAddressDesc) :  OSErr;
  515.  
  516. VAR
  517.     procSerNum : ProcessSerialNumber;
  518.     
  519. BEGIN
  520.     procSerNum.highLongOfPSN := 0;
  521.     procSerNum.lowLongOfPSN  := kCurrentProcess;
  522.     
  523.     MakeSelfAddress := AECreateDesc(typeProcessSerialNumber,
  524.                                                                     Ptr(@procSerNum),
  525.                                                                     sizeof(procSerNum),
  526.                                                                     selfAddress);
  527.     
  528. END; (* MakeSelfAddress *)
  529.  
  530. {*--------------------------------------------------------------------
  531.     Name : SendAESetObjProp
  532.     Function : Creates a property object from an object,
  533.                          a property type and its data and sends it to
  534.                          the requested address, and cleans up zapping params too
  535.     --------------------------------------------------------------------*}
  536.  
  537. FUNCTION SendAESetObjProp(VAR theObj    : AEDesc; 
  538.                                                     theProp        : DescType;
  539.                                                     VAR theData    : AEDesc;
  540.                                                     VAR toWhom  : AEAddressDesc):OSErr;
  541.                                                      
  542. VAR propObjSpec        :    AEDesc;
  543.         myAppleEvent    :    AppleEvent;
  544.         defReply            :    AppleEvent;
  545.         myErr         : OSErr;
  546.         ignoreErr     : OSErr;
  547.         theProperty   : AEDesc;
  548.         
  549. BEGIN
  550.   { create an object spec that represents the property of the given object }
  551.   
  552.     myErr := AECreateDesc(typeType,
  553.                                                 @theProp,
  554.                                                 sizeof(theProp),
  555.                                                 theProperty);
  556.     
  557.     IF (myErr=noErr) THEN
  558.         myErr:= CreateObjSpecifier(cProperty,
  559.                                                              theObj,
  560.                                                              formPropertyID,
  561.                                                              theProperty,
  562.                                                              TRUE,
  563.                                                              propObjSpec);    
  564.       
  565.   { create event }
  566.     
  567.     IF (myErr=noErr) THEN
  568.         myErr := AECreateAppleEvent(kAECoreSuite,
  569.                                                                 kAESetData,
  570.                                                                 toWhom,
  571.                                                                 0,
  572.                                                                 0,
  573.                                                                 myAppleEvent);
  574.       
  575.   { add prop obj spec to the event }
  576.     
  577.     IF (myErr=noErr) THEN
  578.         myErr := AEPutParamDesc(myAppleEvent,keyDirectObject,propObjSpec);
  579.   
  580.   { add prop data to the event }
  581.     
  582.     IF (myErr=noErr) THEN
  583.         myErr := AEPutParamDesc(myAppleEvent,keyAEData,theData);
  584.   
  585.   { send event }
  586.     
  587.     IF (myErr=noErr) THEN    
  588.         myErr := AESend(myAppleEvent,
  589.                                         defReply,
  590.                                         kAENoReply+kAEAlwaysInteract,
  591.                                         kAENormalPriority,
  592.                                         kAEDefaultTimeOut,
  593.                                         NIL,
  594.                                         NIL);
  595.   
  596.   ignoreErr := AEDisposeDesc(myAppleEvent);
  597.   
  598.   ignoreErr := AEDisposeDesc(propObjSpec);
  599.     
  600.     ignoreErr := AEDisposeDesc(theData);
  601.     
  602.     ignoreErr := AEDisposeDesc(toWhom);
  603.     
  604.     SendAESetObjProp := myErr;
  605.     
  606. END;    { SendAESetObjProp }
  607.  
  608. {----------------------------------------------------------------------------------------------}
  609. {
  610.     Private AEObject definitions
  611. }
  612. {$S AECommandHandlers}
  613.  
  614. CONST
  615.     typeMyAppl        = 'BAPP'; { sig of my private token type for the app     - appToken   }
  616.   typeMyWndw              =    'BWIN';    { sig of my private token type for windows     - windowToken   }
  617.     typeMyText            =    'BTXT';    { sig of my private token type for text        - textToken     }
  618.     typeMyTextProp    =    'BPRP';    { sig of my private token type for text properties    - textPropToken }
  619.     typeMyWindowProp  =    'WPRP';    { sig of my private token type for window properties  - windowPropToken }
  620.     typeMyApplProp      =    'APRP';    { sig of my private token type for appl properties    - applPropToken }
  621.     typeMyMenu        = 'MTKN'; { sig of my private token type for menus       - menuToken  }
  622.     typeMyMenuItem    = 'ITKN'; { sig of my private token type for menus       - menuItemToken  }
  623.     typeMyMenuProp    = 'MPRP'; { sig of my private token type for menu properties - menuPropToken  }
  624.     typeMyItemProp    = 'IPRP'; { sig of my private token type for menu item properties  - menuItemPropToken  }
  625.  
  626. TYPE 
  627.     (* These are entirely private to our app - used only when resolving the object specifier *)
  628.     
  629.     appToken    = ProcessSerialNumber;
  630.     
  631.     applPropToken = RECORD
  632.                                             tokenApplToken    : appToken;
  633.                                             tokenApplProperty : DescType;
  634.                                         END;
  635.                                         
  636.     windowToken = WindowPtr;
  637.     
  638.     windowPropToken = RECORD
  639.                                             tokenWindowToken : WindowToken;
  640.                                             tokenProperty    : DescType;
  641.                                         END;
  642.     
  643.     textToken   = RECORD
  644.                                     tokenWindow : WindowPtr;
  645.                                     tokenOffset : INTEGER;
  646.                                     tokenLength : INTEGER;
  647.                                 END;
  648.                                 
  649.     textPropToken = RECORD
  650.                                         propertyTextToken : textToken;
  651.                                         propertyProperty    : DescType;
  652.                                     END;
  653.                                     
  654. (* Tokens related to menus *)
  655.  
  656. MenuToken = RECORD
  657.                             theTokenMenu : MenuHandle;
  658.                             theTokenID   : INTEGER;
  659.                         END;
  660.  
  661. MenuItemToken = RECORD
  662.                                     theMenuToken : MenuToken;
  663.                                     theTokenItem : INTEGER;
  664.                                 END;
  665.  
  666. MenuPropToken = RECORD
  667.                                     theMenuToken : MenuToken;
  668.                                     theMenuProp  : DescType;
  669.                                 END;
  670.  
  671. MenuItemPropToken = RECORD
  672.                                             theItemToken : MenuItemToken;
  673.                                             theItemProp  : DescType;
  674.                                         END;
  675.  
  676. FUNCTION GotRequiredParams(theAppleEvent: AppleEvent):  OSErr;
  677.     VAR myErr            : OSErr;
  678.         returnedType: DescType;
  679.         actSize            : Size;
  680.         
  681.     BEGIN
  682.         { look for the keyMissedKeywordAttr, just to see if it's there }
  683.         
  684.         myErr := AEGetAttributePtr(    theAppleEvent,
  685.                                                                 keyMissedKeywordAttr,
  686.                                                                 typeWildCard,
  687.                                                                 returnedType,
  688.                                                                 NIL,
  689.                                                                 0,
  690.                                                                 actSize);
  691.         
  692.         IF myErr = errAEDescNotFound THEN 
  693.             GotRequiredParams := noErr            { attribute not there means we got all req params }
  694.         ELSE 
  695.             IF myErr = noErr THEN 
  696.                 GotRequiredParams := errAEParamMissed        { attribute there means missed at least one }
  697.             ELSE 
  698.                 GotRequiredParams := myErr;        { some unexpected arror in looking for the attribute }
  699.     END;    (* GotReqiredParams *)
  700.  
  701. {*--------------------------------------------------------------------
  702.     Name : SetSelectionOfAppleEventDirectObject
  703.     Function : Resolves the Direct Object into a text token and
  704.                          sets the selection of the specified document to that
  705.                          specified in the direct object.
  706.                          Returns the doc and TEHandle chosen.
  707.     --------------------------------------------------------------------*}
  708.  
  709. FUNCTION SetSelectionOfAppleEventDirectObject(theAppleEvent        : AppleEvent;
  710.                                                                                              VAR theDocument : DPtr;
  711.                                                                                             VAR theHTE            : TEHandle):OsErr;
  712.     VAR myErr              : OSErr;
  713.             returnedType : DescType;
  714.             actSize      : longInt;
  715.             myTextToken  : TextToken;
  716.             paramErr     : OSErr;
  717.             
  718.     BEGIN
  719.         SetSelectionOfAppleEventDirectObject := noErr;
  720.         
  721.         paramErr := AEGetParamPtr(theAppleEvent,
  722.                                                             keyDirectObject,
  723.                                                             typeMyText,
  724.                                                             returnedType,
  725.                                                       @myTextToken,
  726.                                                             SizeOf(myTextToken),
  727.                                                             actSize);
  728.               
  729.         myErr := GotRequiredParams(theAppleEvent);
  730.         
  731.         { now let's work on the direct object, if any }
  732.         
  733.         IF (paramErr = errAEDescNotFound) THEN
  734.             BEGIN
  735.                 { no direct object; check we have a window }
  736.                 IF (FrontWindow = NIL) THEN
  737.                     BEGIN
  738.                         SetSelectionOfAppleEventDirectObject := -1700;
  739.                         Exit(SetSelectionOfAppleEventDirectObject);
  740.                     END;
  741.                 theDocument := DPtrFromWindowPtr(FrontWindow);
  742.                 theHTE      := theDocument^.theText;
  743.             END;
  744.         
  745.         IF (paramErr = noErr) THEN
  746.             BEGIN
  747.                 { got a text token }
  748.                 
  749.                 theDocument := DPtrFromWindowPtr(myTextToken.tokenWindow);
  750.                 theHTE      := theDocument^.theText;
  751.                 
  752.                 TESetSelect(myTextToken.tokenOffset-1,
  753.                             myTextToken.tokenOffset+myTextToken.tokenLength-1,
  754.                                         theHTE);
  755.                 
  756.             END;
  757.             
  758.         IF (paramErr<>noErr) AND
  759.            (paramErr<>errAEDescNotFound) THEN
  760.              BEGIN
  761.                  theDocument := DPtrFromWindowPtr(FrontWindow);
  762.                  theHTE      := theDocument^.theText;
  763.              END;
  764.  
  765.         SetSelectionOfAppleEventDirectObject := myErr;
  766.              
  767.     END; (* SetSelectionOfAppleEventDirectObject *)
  768.     
  769. {*--------------------------------------------------------------------
  770.     Name             : SetSelectionOfAppleEventDirectObject
  771.     Function     : Resolves the whatObject type of the AppleEvent into a text
  772.                              token and sets the selection to be that specified in the
  773.                              text token.
  774.                              Returns the doc and TEHandle chosen.
  775.     --------------------------------------------------------------------*}
  776.  
  777. FUNCTION SetSelectionOfAppleEventObject(whatObject             : OSType;
  778.                                                                                 theAppleEvent        : AppleEvent;
  779.                                                                                  VAR theDocument : DPtr;
  780.                                                                                 VAR theHTE            : TEHandle):OsErr;
  781.     VAR returnedType : DescType;
  782.             actSize      : longInt;
  783.             myTextToken  : TextToken;
  784.             paramErr     : OSErr;
  785.             
  786.     BEGIN        
  787.         paramErr := AEGetParamPtr(theAppleEvent,
  788.                                                             whatObject,
  789.                                                             typeMyText,
  790.                                                             returnedType,
  791.                                                       @myTextToken,
  792.                                                             SizeOf(myTextToken),
  793.                                                             actSize);
  794.                                       
  795.         IF (paramErr = noErr) THEN
  796.             BEGIN
  797.                 { got a text token }
  798.                 
  799.                 theDocument := DPtrFromWindowPtr(myTextToken.tokenWindow);
  800.                 theHTE      := theDocument^.theText;
  801.                 
  802.                 TESetSelect(myTextToken.tokenOffset-1,
  803.                             myTextToken.tokenOffset+myTextToken.tokenLength-1,
  804.                                         theHTE);
  805.                 
  806.             END;
  807.                              
  808.         SetSelectionOfAppleEventObject := paramErr;
  809.                  
  810.     END; (* SetSelectionOfAppleEventObject *)
  811.  
  812. { -----------------------------------------------------------------------
  813.         Name:             DoCutEdit
  814.         Purpose:        Performs a cut text operation on the current text selection
  815.      -----------------------------------------------------------------------*}
  816.     
  817. FUNCTION DoCutEdit(theAppleEvent, reply : AppleEvent; refCon : LONGINT): OSErr;
  818.     VAR myErr              : OSErr;
  819.             theHTE             : TEHandle;
  820.             theDocument  : DPtr;
  821.     
  822.     BEGIN
  823.         myErr := SetSelectionOfAppleEventDirectObject(theAppleEvent,theDocument,theHTE);
  824.         
  825.         IF (myErr=noErr) THEN
  826.             BEGIN
  827.               DoTECutSectionRecalc(theDocument);
  828.             
  829.                 myErr := ZeroScrap;
  830.                 TECut(theHTE);
  831.                 AdjustScrollbars(theDocument, FALSE);
  832.                 DrawPageExtras(theDocument);
  833.                 theDocument^.dirty := TRUE;                
  834.             END;
  835.             
  836.         DoCutEdit := myErr;
  837.     END; (* DoCutEdit *)
  838.     
  839. { -----------------------------------------------------------------------
  840.         Name:             DoCopyEdit
  841.         Purpose:        Performs a copy text operation on the text selection specified
  842.                                 by the appleEvent direct object (if any)
  843.      -----------------------------------------------------------------------*}
  844.     
  845. FUNCTION DoCopyEdit(theAppleEvent, reply : AppleEvent; refCon : LONGINT): OSErr;
  846.     VAR myErr              : OSErr;
  847.             theHTE             : TEHandle;
  848.             theDocument  : DPtr;
  849.     BEGIN
  850.         (*
  851.                 Here we extract the information about what to copy from the
  852.                 directObject - if any
  853.         *)
  854.         
  855.         myErr := SetSelectionOfAppleEventDirectObject(theAppleEvent,theDocument,theHTE);
  856.         
  857.         IF myErr=noErr THEN
  858.             BEGIN
  859.                 myErr := ZeroScrap;
  860.                 TECopy(theHTE);     
  861.             END;
  862.             
  863.         IF (myErr=noErr) THEN
  864.             IF (SetSelectionOfAppleEventObject(keyAEContainer, 
  865.                                                                                 theAppleEvent,
  866.                                                                                 theDocument,
  867.                                                                                 theHTE) = noErr) THEN
  868.                 BEGIN
  869.                     DoTEPasteSectionRecalc(theDocument);
  870.                     
  871.                     TEStylPaste(theHTE);
  872.                     
  873.                     AdjustScrollBars(theDocument, FALSE);
  874.                     
  875.                     DrawPageExtras(theDocument);
  876.                     
  877.                     theDocument^.dirty := TRUE;
  878.                 END;
  879.             
  880.         DoCopyEdit := myErr;
  881.     END; (* DoCopyEdit *)
  882.     
  883. { -----------------------------------------------------------------------
  884.         Name:             DoPasteEdit
  885.         Purpose:        Performs a paste text operation on the text selection specified
  886.                                 by the appleEvent direct object (if any)
  887.      -----------------------------------------------------------------------*}
  888.     
  889. FUNCTION DoPasteEdit(theAppleEvent, reply : AppleEvent; refCon : LONGINT): OSErr;
  890.     VAR myErr              : OSErr;
  891.             theHTE             : TEHandle;
  892.             theDocument  : DPtr;
  893.     BEGIN
  894.         myErr := SetSelectionOfAppleEventDirectObject(theAppleEvent,theDocument,theHTE);
  895.         
  896.         IF myErr=noErr THEN
  897.             BEGIN
  898.                 DoTEPasteSectionRecalc(theDocument);
  899.                 
  900.                 TEStylPaste(theHTE);     
  901.  
  902.                 AdjustScrollbars(theDocument, FALSE);
  903.                 
  904.                 DrawPageExtras(theDocument);
  905.  
  906.                 theDocument^.dirty := TRUE;
  907.             END;
  908.             
  909.         DoPasteEdit := myErr;
  910.     END; (* DoPasteEdit *)
  911.     
  912. { -----------------------------------------------------------------------
  913.         Name:             DoDeleteEdit
  914.         Purpose:        Performs a delete text operation on the selection specified
  915.                                 by the appleEvent direct object (if any)
  916.      -----------------------------------------------------------------------*}
  917.      
  918. FUNCTION DoDeleteEdit(theAppleEvent,reply : AppleEvent; refcon : longInt):OSErr;
  919.     VAR myErr              : OSErr;
  920.             theHTE             : TEHandle;
  921.             theDocument  : DPtr;
  922.             
  923.     BEGIN
  924.         myErr := SetSelectionOfAppleEventDirectObject(theAppleEvent,theDocument,theHTE);
  925.         
  926.         IF myErr=noErr THEN
  927.             BEGIN
  928.                 DoTEDeleteSectionRecalc(theDocument);
  929.                         
  930.                 TEDelete(theHTE);     
  931.                 
  932.                 theDocument^.dirty := TRUE;
  933.                 AdjustScrollbars(theDocument, FALSE);
  934.                 DrawPageExtras(theDocument);
  935.             END;
  936.                     
  937.         DoDeleteEdit := myErr;
  938.     END; (*DoDeleteEdit*)
  939.     
  940. { -----------------------------------------------------------------------
  941.         Name:             SetTheFontOfTheTokenText
  942.         Purpose:        Sets the font of the text specified by theTextToken to 
  943.                                 the font in name.
  944.      -----------------------------------------------------------------------*}
  945.      
  946. PROCEDURE SetTheFontOfTheTokenText(theTextToken : textToken; name:Str255);
  947.     VAR theDocument : DPtr;
  948.             theNumber   : INTEGER;
  949.             theItem     : INTEGER;
  950.             oldPort     : GrafPtr;
  951.             newStyle        : textStyle;
  952.     
  953.     BEGIN
  954.         GetPort(oldPort);
  955.         
  956.         theDocument := DPtrFromWindowPtr(theTextToken.tokenWindow);
  957.                 
  958.         GetFNum(name, theNumber);
  959.         
  960.         theItem := ItemForNamedFont(name); (* returns 0 if failed - i.e. SystemFont *)
  961.         
  962.         IF gFontMItem <> 0 THEN
  963.             CheckItem(myMenus[fontM], gFontMItem, FALSE);
  964.         
  965.         gFontMItem := theItem;
  966.         CheckItem(myMenus[fontM], gFontMItem, TRUE);
  967.         
  968.         theDocument^.theFont := theNumber;
  969.         
  970.         TESetSelect(theTextToken.tokenOffset-1,
  971.                                 theTextToken.tokenOffset+theTextToken.tokenLength-1,
  972.                                 theDocument^.theText);
  973.         
  974.         newStyle.tsFont := theNumber;
  975.         TESetStyle(doFont, newStyle, TRUE, theDocument^.theText); 
  976.         
  977.         AdjustScrollbars(theDocument, FALSE);
  978.         DrawPageExtras(theDocument);
  979.         theDocument^.dirty := TRUE;
  980.         
  981.         SetPort(oldPort);
  982.     END; (* SetTheFontOfTheTokenText *)
  983.     
  984. { -----------------------------------------------------------------------
  985.         Name:             SetTheSizeOfTheTokenText
  986.         Purpose:        Sets the size of the text specified by theTextToken to 
  987.                                 the size in theSize.
  988.      -----------------------------------------------------------------------*}
  989.      
  990. PROCEDURE SetTheSizeOfTheTokenText(theTextToken : textToken; theSize:INTEGER);
  991.     VAR theDocument : DPtr;
  992.             oldPort     : GrafPtr;
  993.             newStyle    : textStyle;
  994.     
  995.     BEGIN
  996.         GetPort(oldPort);
  997.         
  998.         theDocument := DPtrFromWindowPtr(theTextToken.tokenWindow);
  999.                                 
  1000.         theDocument^.theSize := theSize;
  1001.         
  1002.         TESetSelect(theTextToken.tokenOffset-1,
  1003.                                 theTextToken.tokenOffset+theTextToken.tokenLength-1,
  1004.                                 theDocument^.theText);
  1005.         
  1006.         newStyle.tsSize := theSize;
  1007.         TESetStyle(doSize, newStyle, TRUE, theDocument^.theText); 
  1008.         
  1009.         AdjustScrollbars(theDocument, FALSE);
  1010.         DrawPageExtras(theDocument);
  1011.         theDocument^.dirty := TRUE;
  1012.         
  1013.         SetPort(oldPort);
  1014.     END; (* SetTheSizeOfTheTokenText *)
  1015.     
  1016. { -----------------------------------------------------------------------
  1017.         Name:             SetTheStyleOfTheTokenText
  1018.         Purpose:        Sets the style of the text specified by theTextToken to 
  1019.                                 the style in theStyle.
  1020.      -----------------------------------------------------------------------*}
  1021.      
  1022. PROCEDURE SetTheStyleOfTheTokenText(theTextToken : textToken; 
  1023.                                     onStyle      : Style;
  1024.                                                                         offStyle     : Style);
  1025.     VAR theDocument   : DPtr;
  1026.             oldPort       : GrafPtr;
  1027.             newStyle          : TextStyle;
  1028.           mode          : INTEGER;
  1029.           wasContinuous : Boolean;
  1030.         
  1031.     BEGIN
  1032.         GetPort(oldPort);
  1033.         
  1034.         theDocument := DPtrFromWindowPtr(theTextToken.tokenWindow);
  1035.                                 
  1036.         theDocument^.theStyle := onStyle;
  1037.         TESetSelect(theTextToken.tokenOffset-1,
  1038.                                 theTextToken.tokenOffset+theTextToken.tokenLength-1,
  1039.                                 theDocument^.theText);
  1040.                                 
  1041.         { Check to see if off styles are on for whole selection }
  1042.         mode := doFace;
  1043.         
  1044.         wasContinuous := TEContinuousStyle(mode, newStyle, theDocument^.theText);
  1045.         
  1046.         if ((newStyle.tsFace * offStyle) <> offStyle) THEN (*not off styles are on for all *)
  1047.             BEGIN
  1048.               { switch on across board so that toggle off will clear all }
  1049.             newStyle.tsFace  := offStyle - (newStyle.tsFace * offStyle);
  1050.                 TESetStyle(doFace+doToggle, newStyle, false, theDocument^.theText); 
  1051.             END;
  1052.         
  1053.         newStyle.tsFace :=  offStyle;
  1054.         TESetStyle(doFace+doToggle, newStyle, (onStyle=[]), theDocument^.theText); (* Toggle all to off *)
  1055.         
  1056.         mode := doFace;
  1057.         
  1058.         IF (onStyle<>[]) THEN
  1059.           BEGIN
  1060.                 wasContinuous := TEContinuousStyle(mode, newStyle, theDocument^.theText);
  1061.                 IF ((newStyle.tsFace * onStyle) <> onStyle) THEN (* are they on for only a few chars *)
  1062.                     BEGIN
  1063.                         (* Need to make all chars have these characteristics *)
  1064.                     
  1065.                         newStyle.tsFace := onStyle - (newStyle.tsFace * onStyle); (* take out those continuous *)
  1066.                         TESetStyle(doFace+doToggle, newStyle, true, theDocument^.theText);
  1067.                     END
  1068.                 ELSE
  1069.                     TESetStyle(0, newStyle, true, theDocument^.theText); (* Just Draw it, no changes *)
  1070.           END;
  1071.         
  1072.         AdjustScrollbars(theDocument, FALSE);
  1073.         DrawPageExtras(theDocument);
  1074.         theDocument^.dirty := TRUE;
  1075.         
  1076.         SetPort(oldPort);
  1077.     END; (* SetTheStyleOfTheTokenText *)
  1078.  
  1079. { -----------------------------------------------------------------------
  1080.         Name:             SetWindowProperty
  1081.         Purpose:        Sets the window property specified in theWPTokenDesc to
  1082.                                 be that supplied in dataDesc.
  1083.      -----------------------------------------------------------------------*}
  1084.      
  1085. FUNCTION SetWindowProperty(theWPTokenDesc:AEDesc; dataDesc:AEDesc):OSErr;
  1086.     VAR theNewName           : Str255;
  1087.             theDocument        : DPtr;
  1088.             err                           : OSErr;
  1089.             ignoreErr            : OSErr;
  1090.             thePosnRect        : Rect;
  1091.             theBoolean         : Boolean;
  1092.             theHTE                   : TEHandle;
  1093.             oldPort                   : GrafPtr;
  1094.             thePosn            : Point;
  1095.             theTHPrint           : THPrint;
  1096.             theWindowPropToken : windowPropToken;
  1097.             newDesc            : AEDesc;
  1098.             tokenSize          : Size;
  1099.             tokenDesc          : AEDesc;
  1100.             myTextToken        : TextToken;
  1101.             hOffset            : INTEGER;
  1102.             vOffset            : INTEGER;
  1103.     
  1104.     BEGIN
  1105.         err := AECoerceDesc(theWPTokenDesc,typeMyWindowProp, newDesc);
  1106.         
  1107.         IF (err<>noErr) THEN
  1108.             BEGIN
  1109.                 SetWindowProperty := err;
  1110.                 Exit(SetWindowProperty);
  1111.             END;
  1112.  
  1113.         GetRawDataFromDescriptor(newDesc,
  1114.                                                          @theWindowPropToken,
  1115.                                                          SizeOf(theWindowPropToken),
  1116.                                                          tokenSize);
  1117.                                                          
  1118.         err := AEDisposeDesc(newDesc);
  1119.             
  1120.         GetPort(oldPort);
  1121.         SetPort(theWindowPropToken.tokenWindowToken);
  1122.         
  1123.         theDocument := DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  1124.         
  1125.         IF (theWindowPropToken.tokenProperty = pName) THEN
  1126.             BEGIN
  1127.                 err := GetPStringFromDescriptor(dataDesc, theNewName);
  1128.                 IF (err=noErr) THEN
  1129.                     IF (theNewName = '') THEN
  1130.                         err := errAEWrongDataType
  1131.                     ELSE
  1132.                         BEGIN
  1133.                             SetWTitle(theWindowPropToken.tokenWindowToken, theNewName);
  1134.                             theDocument^.theFileName := theNewName;
  1135.                             theDocument^.dirty := TRUE;
  1136.                         END;
  1137.             END;
  1138.             
  1139.         IF (theWindowPropToken.tokenProperty = pText) THEN
  1140.             BEGIN
  1141.                 theHTE:=theDocument^.theText;
  1142.                 TESetSelect(0, 32000, theHTE);
  1143.                 
  1144.                 DoTEDeleteSectionRecalc(theDocument);
  1145.                 TEDelete(theHTE);
  1146.                 
  1147.                 err := GetStyledTextFromDescIntoTEHandle(dataDesc, theHTE);
  1148.  
  1149.                 theDocument^.dirty := true;
  1150.             END;
  1151.             
  1152.         IF (theWindowPropToken.tokenProperty = pBounds) THEN
  1153.             BEGIN
  1154.                 err := GetRectFromDescriptor(dataDesc, thePosnRect);
  1155.                  WITH thePosnRect DO
  1156.                     BEGIN
  1157.                     { the rectangle is for the structure region, and is in global coordinates }
  1158.                     { MoveWindow and SizeWindow apply to the content region, so we have to massage a little }
  1159.                     { the massage is specific to the type of window we are using }
  1160.                     
  1161.                         thePosnRect.top    := thePosnRect.top +
  1162.                                                                     WindowPeek(theWindowPropToken.tokenWindowToken)^.contRgn^^.rgnBBox.top -
  1163.                                                                     WindowPeek(theWindowPropToken.tokenWindowToken)^.strucRgn^^.rgnBBox.top;
  1164.                                                                     
  1165.                         thePosnRect.left   := thePosnRect.left +
  1166.                                                                     WindowPeek(theWindowPropToken.tokenWindowToken)^.contRgn^^.rgnBBox.left -
  1167.                                                                     WindowPeek(theWindowPropToken.tokenWindowToken)^.strucRgn^^.rgnBBox.left;
  1168.                                                                     
  1169.                         thePosnRect.bottom := thePosnRect.bottom +
  1170.                                                                     WindowPeek(theWindowPropToken.tokenWindowToken)^.contRgn^^.rgnBBox.bottom -
  1171.                                                                     WindowPeek(theWindowPropToken.tokenWindowToken)^.strucRgn^^.rgnBBox.bottom;
  1172.                                                                     
  1173.                         thePosnRect.right  := thePosnRect.right +
  1174.                                                                     WindowPeek(theWindowPropToken.tokenWindowToken)^.contRgn^^.rgnBBox.right -
  1175.                                                                     WindowPeek(theWindowPropToken.tokenWindowToken)^.strucRgn^^.rgnBBox.right;
  1176.                                                                 
  1177.                         
  1178.                         IF EmptyRect(thePosnRect) THEN
  1179.                             err := errAECorruptData
  1180.                         ELSE
  1181.                             BEGIN
  1182.                                 MoveWindow(theWindowPropToken.tokenWindowToken,
  1183.                                                      thePosnRect.left,
  1184.                                                      thePosnRect.top,
  1185.                                                      FALSE);
  1186.                                 SizeWindow(theWindowPropToken.tokenWindowToken,
  1187.                                                      thePosnRect.right- thePosnRect.left,
  1188.                                                      thePosnRect.bottom-thePosnRect.top,
  1189.                                                      TRUE);
  1190.                                 ResizeWindow(theDocument);
  1191.                             END;
  1192.                 END;
  1193.             END;
  1194.  
  1195.         IF (theWindowPropToken.tokenProperty = pPosition) THEN
  1196.             BEGIN
  1197.                 err := GetPointFromDescriptor(dataDesc, thePosn);
  1198.                 { the point is for the structure region, and is in global coordinates }
  1199.                 { MoveWindow applies to the content region, so we have to massage a little }
  1200.                                                             
  1201.                 hOffset :=    WindowPeek(theWindowPropToken.tokenWindowToken)^.contRgn^^.rgnBBox.left -
  1202.                                         WindowPeek(theWindowPropToken.tokenWindowToken)^.strucRgn^^.rgnBBox.left;
  1203.                 
  1204.                 vOffset :=    WindowPeek(theWindowPropToken.tokenWindowToken)^.contRgn^^.rgnBBox.top -
  1205.                                         WindowPeek(theWindowPropToken.tokenWindowToken)^.strucRgn^^.rgnBBox.top;
  1206.                 
  1207.                 thePosn.v := thePosn.v + vOffset;
  1208.                 thePosn.h := thePosn.h + hOffset;
  1209.                 
  1210.                 MoveWindow(theWindowPropToken.tokenWindowToken,
  1211.                                      thePosn.h,
  1212.                                      thePosn.v,
  1213.                                      FALSE);
  1214.                                      
  1215.                 ResizeWindow(theDocument);
  1216.             END;
  1217.  
  1218.         IF (theWindowPropToken.tokenProperty = pIsZoomed) THEN
  1219.             BEGIN
  1220.                 err := GetBooleanFromDescriptor(dataDesc, theBoolean);
  1221.                 IF theBoolean THEN
  1222.                     ZoomWindow(thePort,
  1223.                                          inZoomOut,
  1224.                                          FALSE)
  1225.                 ELSE
  1226.                     ZoomWindow(thePort,
  1227.                                          inZoomIn,
  1228.                                          FALSE);
  1229.                                                          
  1230.                 ResizeWindow(theDocument);
  1231.             END;
  1232.             
  1233.         IF (theWindowPropToken.tokenProperty = pVisible) THEN
  1234.             BEGIN
  1235.                 err := GetBooleanFromDescriptor(dataDesc, theBoolean);
  1236.                 IF theBoolean THEN
  1237.                     ShowWindow(theWindowPropToken.tokenWindowToken)
  1238.                 ELSE
  1239.                     HideWindow(theWindowPropToken.tokenWindowToken);
  1240.             END;
  1241.             
  1242.         IF (theWindowPropToken.tokenProperty = pPageSetup) THEN
  1243.             BEGIN
  1244.                 err := GetTHPrintFromDescriptor(dataDesc,theTHPrint);
  1245.                     
  1246.                 IF (theTHPrint<>NIL) THEN
  1247.                     BEGIN
  1248.                         IF (theDocument^.thePrintSetup <> NIL) THEN
  1249.                             DisposHandle(Handle(theDocument^.thePrintSetup));
  1250.                             
  1251.                         theDocument^.thePrintSetup := theTHPrint;
  1252.                         
  1253.                         ResizePageSetupForDocument(theDocument);
  1254.                     END;
  1255.                                 
  1256.             END;
  1257.             
  1258.         IF (theWindowPropToken.tokenProperty = pShowBorders) THEN
  1259.             BEGIN
  1260.                 err := GetBooleanFromDescriptor(dataDesc, theBoolean);
  1261.                 theDocument^.showBorders := theBoolean;
  1262.                 IF theBoolean THEN
  1263.                     DrawPageExtras(theDocument) (* Does the clipping as well as drawing borders/page breaks *)
  1264.                 ELSE
  1265.                     InvalidateDocument(theDocument);
  1266.             END;
  1267.             
  1268.         IF (theWindowPropToken.tokenProperty = pSelection) THEN
  1269.             BEGIN                    
  1270.                 err := AECoerceDesc(dataDesc, typeMyText, tokenDesc);
  1271.                 
  1272.                 GetRawDataFromDescriptor(tokenDesc,
  1273.                                                                  Ptr(@myTextToken),
  1274.                                                                  sizeof(myTextToken),
  1275.                                                                  tokenSize);
  1276.                                                                                  
  1277.                 ignoreErr := AEDisposeDesc(tokenDesc);
  1278.                 
  1279.                 IF (err = noErr) THEN
  1280.                     BEGIN
  1281.                         (* got a text token *)
  1282.                         
  1283.                         theDocument := DPtrFromWindowPtr(myTextToken.tokenWindow);
  1284.                         theHTE      := theDocument^.theText;
  1285.                         
  1286.                         TESetSelect(myTextToken.tokenOffset-1,
  1287.                                                 myTextToken.tokenOffset+myTextToken.tokenLength-1,
  1288.                                                 theHTE);
  1289.                     END;
  1290.             END;
  1291.             
  1292.         IF (theWindowPropToken.tokenProperty = pIndex)       OR
  1293.              (theWindowPropToken.tokenProperty = pIsModal)     OR
  1294.              (theWindowPropToken.tokenProperty = pIsResizable) OR
  1295.              (theWindowPropToken.tokenProperty = pHasTitleBar) OR
  1296.              (theWindowPropToken.tokenProperty = pHasCloseBox) OR
  1297.              (theWindowPropToken.tokenProperty = pIsFloating)  OR
  1298.              (theWindowPropToken.tokenProperty = pIsZoomable)  OR
  1299.              (theWindowPropToken.tokenProperty = pIsModified)  THEN
  1300.             BEGIN
  1301.                 err := errAEEventNotHandled; (* We don't allow these to be set *)
  1302.             END;
  1303.         
  1304.         SetPort(oldPort);
  1305.         
  1306.         SetWindowProperty := err;
  1307.                 
  1308.     END; (* SetWindowProperty *)
  1309.     
  1310. { -----------------------------------------------------------------------
  1311.         Name:             AddDescStyleItem
  1312.         Purpose:        Adds the kAEXXXX style to theStyle.
  1313.      -----------------------------------------------------------------------*}
  1314.      
  1315. PROCEDURE AddDescStyleItem(theDesc: DescType; VAR theStyle:Style);
  1316.     VAR ctr             : StyleItem;
  1317.     
  1318.     BEGIN
  1319.         IF theDesc = kAEBold THEN
  1320.             theStyle := theStyle+[bold]
  1321.         ELSE
  1322.         IF theDesc = kAEItalic THEN
  1323.             theStyle := theStyle+[italic]
  1324.         ELSE
  1325.         IF theDesc = kAEUnderline THEN
  1326.             theStyle := theStyle+[underline]
  1327.         ELSE
  1328.         IF theDesc = kAEOutline THEN
  1329.             theStyle := theStyle+[outline]
  1330.         ELSE
  1331.         IF theDesc = kAEShadow THEN
  1332.             theStyle := theStyle+[shadow]
  1333.         ELSE
  1334.         IF theDesc = kAECondensed THEN
  1335.             theStyle := theStyle+[condense]
  1336.         ELSE
  1337.         IF theDesc = kAEExpanded THEN
  1338.             theStyle := theStyle+[extend]
  1339.         ELSE
  1340.         IF theDesc = kAEPlain THEN
  1341.             theStyle := [];
  1342.     END; (* AddDescStyleItem *)
  1343.     
  1344. FUNCTION MakeStyleFromAEList(styleList : AEDescList; VAR theStyle : Style; VAR hadPlain:BOOLEAN):OSErr;
  1345.     VAR myErr       : OSErr;
  1346.         styleDesc   : DescType;
  1347.             itemsInList : LongInt;
  1348.             actSize     : LongInt;
  1349.             keywd       : AEKeyWord;
  1350.             typeCode    : DescType;
  1351.             
  1352.     BEGIN
  1353.         hadPlain := FALSE;
  1354.         theStyle := [];
  1355.         
  1356.       myErr := AECountItems(styleList, itemsInList);
  1357.         WHILE (itemsInList>0) DO
  1358.           IF (myErr=noErr) THEN
  1359.                 BEGIN
  1360.                     myErr := AEGetNthPtr(styleList,
  1361.                                                              itemsInList,
  1362.                                                              typeEnumerated,
  1363.                                                              keywd,
  1364.                                                              typeCode,
  1365.                                                              @styleDesc,
  1366.                                                              sizeof(styleDesc),
  1367.                                                              actSize);
  1368.                     
  1369.                     AddDescStyleItem(styleDesc, theStyle);
  1370.                     
  1371.                     IF (styleDesc = kAEPlain) THEN
  1372.                       BEGIN
  1373.                             itemsInList := 0;
  1374.                             hadPlain    := TRUE;
  1375.                         END
  1376.                     ELSE
  1377.                       itemsInList := itemsInList - 1;
  1378.                 END;
  1379.                 
  1380.         MakeStyleFromAEList := myErr;
  1381.     END; (*MakeStyleFromAEList*)
  1382.     
  1383. FUNCTION GetTextStyles(dataDesc:AEDesc; VAR onStyles:Style; VAR offStyles:Style):OSErr;
  1384.     VAR myErr     : OSErr;
  1385.         ignoreErr : OSErr;
  1386.             textSDesc : AEDescList;
  1387.             onDesc    : AEDescList;
  1388.             offDesc   : AEDescList;
  1389.             hadPlain  : BOOLEAN;
  1390.             
  1391.     BEGIN
  1392.       textSDesc.dataHandle := NIL;
  1393.       onDesc.dataHandle    := NIL;
  1394.       offDesc.dataHandle   := NIL;
  1395.         
  1396.         onStyles  := [];
  1397.         offStyles := [];
  1398.         
  1399.       myErr := AECoerceDesc(dataDesc, typeAERecord, textSDesc);
  1400.         
  1401.         IF (myErr=noErr) THEN
  1402.           myErr := AEGetKeyDesc(textSDesc, keyAEOnStyles, typeAEList, onDesc);
  1403.         
  1404.         IF (myErr=noErr) THEN
  1405.           myErr := AEGetKeyDesc(textSDesc, keyAEOffStyles, typeAEList, offDesc);
  1406.         
  1407.         IF (myErr=noErr) THEN
  1408.             myErr := MakeStyleFromAEList(onDesc, onStyles, hadPlain);
  1409.         
  1410.         IF (hadPlain) THEN
  1411.             offStyles := [bold,italic,underline,outline,shadow,condense,extend]
  1412.         ELSE
  1413.             BEGIN
  1414.                 IF (myErr=noErr) THEN
  1415.                     myErr := MakeStyleFromAEList(offDesc, offStyles, hadPlain);
  1416.                     
  1417.                 IF (hadPlain) THEN
  1418.                     myErr := errAEEventFailed;
  1419.             END;
  1420.             
  1421.         IF (textSDesc.dataHandle<>NIL) THEN
  1422.             ignoreErr := AEDisposeDesc(textSDesc);
  1423.             
  1424.         IF (onDesc.dataHandle<>NIL) THEN
  1425.             ignoreErr := AEDisposeDesc(onDesc);
  1426.             
  1427.         IF (offDesc.dataHandle<>NIL) THEN
  1428.             ignoreErr := AEDisposeDesc(offDesc);
  1429.         
  1430.         GetTextStyles := myErr;
  1431.     END; (* GetTextStyles *)
  1432.     
  1433. { -----------------------------------------------------------------------
  1434.         Name:             SetTextProperty
  1435.         Purpose:        Sets the text property specfied by theTextPropToken to
  1436.                                 that in dataDesc.
  1437.      -----------------------------------------------------------------------*}
  1438.      
  1439. FUNCTION SetTextProperty(tokenDesc: AEDesc; dataDesc:AEDesc):OSErr;
  1440.     VAR theHTE                : TEHandle;
  1441.             theDoc           : DPtr;
  1442.             name                   : Str255;
  1443.             theSize               : INTEGER;
  1444.             myErr                   : OSErr;
  1445.           onStyle          : Style;
  1446.           offStyle         : Style;
  1447.           theTextPropToken : textPropToken;
  1448.           newDesc          : AEDesc;
  1449.           tokenSize        : Size;
  1450.             
  1451.     BEGIN
  1452.       newDesc.dataHandle := NIL;
  1453.         
  1454.       myErr := AECoerceDesc(tokenDesc,typeMyTextProp, newDesc);
  1455.         IF (myErr = noErr) THEN
  1456.             BEGIN
  1457.                 GetRawDataFromDescriptor(newDesc,
  1458.                                                                  @theTextPropToken,
  1459.                                                                  SizeOf(theTextPropToken),
  1460.                                                                  tokenSize);
  1461.                 myErr := AEDisposeDesc(newDesc);
  1462.             END
  1463.         ELSE
  1464.             BEGIN
  1465.                 SetTextProperty := myErr;
  1466.               Exit(SetTextProperty);
  1467.             END;
  1468.             
  1469.         theDoc := DPtrFromWindowPtr(theTextPropToken.propertyTextToken.tokenWindow);
  1470.         theDoc^.dirty := TRUE;
  1471.         
  1472.         IF (theTextPropToken.propertyProperty = pText) THEN
  1473.             BEGIN
  1474.                 theHTE := theDoc^.theText;
  1475.                 TESetSelect(theTextPropToken.propertyTextToken.tokenOffset-1,
  1476.                                         theTextPropToken.propertyTextToken.tokenOffset+
  1477.                                                                 theTextPropToken.propertyTextToken.tokenLength-1,
  1478.                                         theHTE);
  1479.                 
  1480.                 DoTEDeleteSectionRecalc(theDoc);
  1481.                 TEDelete(theHTE);
  1482.                 
  1483.                 myErr := GetStyledTextFromDescIntoTEHandle(dataDesc, theHTE);
  1484.  
  1485.                 SetTextProperty := myErr;
  1486.                 Exit(SetTextProperty);
  1487.             END;
  1488.             
  1489.         IF (theTextPropToken.propertyProperty = pFont) THEN
  1490.             BEGIN                    
  1491.                 myErr := GetPStringFromDescriptor(dataDesc, name);
  1492.                 
  1493.                 SetTheFontOfTheTokenText(theTextPropToken.propertyTextToken,
  1494.                                                                  name);
  1495.                 
  1496.                 SetTextProperty:=noErr;
  1497.                 Exit(SetTextProperty);
  1498.             END;
  1499.             
  1500.         IF (theTextPropToken.propertyProperty = pPointSize) THEN
  1501.             BEGIN
  1502.                 myErr := GetIntegerFromDescriptor(dataDesc, theSize);
  1503.                 SetTheSizeOfTheTokenText(theTextPropToken.propertyTextToken,
  1504.                                                                  theSize);
  1505.                 
  1506.                 SetTextProperty := noErr;
  1507.                 Exit(SetTextProperty);
  1508.             END;
  1509.             
  1510.         IF (theTextPropToken.propertyProperty = pTextStyles) THEN
  1511.             BEGIN
  1512.                 onStyle  := [];
  1513.                 offStyle := [];
  1514.                 
  1515.                 myErr := GetTextStyles(dataDesc, onStyle, offStyle);
  1516.  
  1517.         IF (onStyle*offStyle<>[]) THEN
  1518.                     myErr := errAEEventFailed
  1519.                 ELSE
  1520.                     SetTheStyleOfTheTokenText(theTextPropToken.propertyTextToken,
  1521.                                                                         onStyle, 
  1522.                                                                         offStyle);
  1523.                 
  1524.                 SetTextProperty := myErr;
  1525.                 Exit(SetTextProperty);
  1526.             END;
  1527.                         
  1528.         SetTextProperty := errAEWrongDataType;
  1529.             
  1530.     END; (* SetTextProperty *)
  1531.     
  1532. { -----------------------------------------------------------------------
  1533.         Name:             HandleSetData
  1534.         Purpose:        Resolves the object into a token (could be one of many) and
  1535.                                 the sets the data of that object to dataDesc.
  1536.      -----------------------------------------------------------------------*}
  1537.      
  1538. FUNCTION HandleSetData(theObj: AEDesc; dataDesc: AEDesc): OSErr;
  1539. VAR myErr                            : OSErr;
  1540.         newDesc                        : AEDesc;
  1541.         theDocument       : DPtr;
  1542.         theHTE                    : TEHandle;
  1543.         theTextToken        : textToken;
  1544.         tokenSize         : Size;
  1545.         objTokenDesc      : AEDesc;
  1546.         ignoreErr         : OSErr;
  1547.         
  1548. BEGIN        
  1549.     objTokenDesc.dataHandle := NIL;
  1550.     newDesc.dataHandle := NIL;
  1551.     
  1552.     (*
  1553.         Coerce theObj into a token which we can use - 
  1554.             then set the property or data for that token
  1555.     *)
  1556.     
  1557.     myErr:= AEResolve(theObj ,kAEIDoMinimum, objTokenDesc);
  1558.     
  1559.     (* We don't actually allow ANY app property setting, but
  1560.        just incase we'll decode looking for an typeMyApplProp and flag an error
  1561.     *)
  1562.   
  1563.     IF (objTokenDesc.descriptorType = typeMyApplProp) OR
  1564.        (objTokenDesc.descriptorType = typeMyMenu    ) OR
  1565.        (objTokenDesc.descriptorType = typeMyMenuProp) OR
  1566.        (objTokenDesc.descriptorType = typeMyMenuItem) OR
  1567.        (objTokenDesc.descriptorType = typeMyItemProp) THEN
  1568.         myErr := errAEWrongDataType
  1569.     ELSE
  1570.     IF (objTokenDesc.descriptorType = typeMyWindowProp) THEN
  1571.         myErr := SetWindowProperty(objTokenDesc, dataDesc)
  1572.     ELSE
  1573.     IF (objTokenDesc.descriptorType = typeMyTextProp) THEN
  1574.         myErr := SetTextProperty(objTokenDesc, dataDesc)
  1575.     ELSE
  1576.     IF (objTokenDesc.descriptorType = typeMyText) THEN
  1577.         IF AECoerceDesc(objTokenDesc,typeMyText,newDesc) = noErr THEN
  1578.             BEGIN
  1579.                 GetRawDataFromDescriptor(newDesc,
  1580.                                                                  @theTextToken,
  1581.                                                                  SizeOf(theTextToken),
  1582.                                                                  tokenSize);
  1583.                                                                  
  1584.                 theDocument := DPtrFromWindowPtr(theTextToken.tokenWindow);
  1585.                 theHTE            := theDocument^.theText;
  1586.                 
  1587.                 TESetSelect(theTextToken.tokenOffset-1,
  1588.                                         theTextToken.tokenOffset+
  1589.                                                                 theTextToken.tokenLength-1,
  1590.                                         theHTE);
  1591.                 
  1592.                 DoTEDeleteSectionRecalc(theDocument);
  1593.                 TEDelete(theHTE);
  1594.                 
  1595.                 myErr := GetStyledTextFromDescIntoTEHandle(dataDesc, theHTE);
  1596.                                 
  1597.                 theDocument^.dirty := TRUE;
  1598.             END;
  1599.             
  1600.     IF (objTokenDesc.dataHandle<>NIL) THEN
  1601.         ignoreErr:=AEDisposeDesc(objTokenDesc);
  1602.     
  1603.     HandleSetData := myErr;
  1604.     
  1605. END;    { HandleSetData }
  1606.  
  1607. (*
  1608.     A few convenient FORWARDS...
  1609. *)
  1610.  
  1611. FUNCTION MakeWindowObj( theWindow  : WindowPtr;
  1612.                                                 VAR dMyDoc : AEDesc):OSErr; FORWARD;
  1613.  
  1614. FUNCTION MakeSelTextObj(theWindow         : WindowPtr;
  1615.                                               theTextEditHandle : TEHandle; 
  1616.                                                 VAR selTextObj    : AEDesc): OSErr;
  1617. VAR 
  1618.     myErr            : OSErr;
  1619.     ignoreErr    : OSErr;
  1620.     dNull     : AEDesc;
  1621.     dMyDoc    : AEDesc;
  1622.     startOfs    :    AEDesc;
  1623.     endOfs        : AEDesc;
  1624.     startObj    :    AEDesc;
  1625.     endObj        : AEDesc;
  1626.     rangeDesc    : AEDesc;
  1627.     startChar    :    LongInt;
  1628.     endChar        :    LongInt;
  1629.     spotFlag    :    BOOLEAN;
  1630.         
  1631. BEGIN
  1632.   myErr := noErr;
  1633.     
  1634.     IF (theWindow=nil) THEN
  1635.         BEGIN
  1636.             MakeSelTextObj:=noErr;
  1637.             Exit(MakeSelTextObj);
  1638.         END;
  1639.         
  1640.     selTextObj.dataHandle := nil;
  1641.     dMyDoc.dataHandle     := nil;
  1642.     startObj.dataHandle   := nil;
  1643.     endObj.dataHandle     := nil;
  1644.     
  1645.   (* 
  1646.         make the window object 
  1647.     *)
  1648.     
  1649.     myErr := MakeWindowObj(theWindow, dMyDoc);
  1650.         
  1651.   IF (myErr=noErr) THEN
  1652.         BEGIN
  1653.             { get the start and end of selection }
  1654.             
  1655.             WITH theTextEditHandle^^ DO
  1656.                 BEGIN
  1657.                     startChar := selStart+1;    { start counting obj's from 1, not 0 }
  1658.                     endChar   := selEnd;
  1659.                     spotFlag  := (selStart = selEnd);
  1660.                 END;
  1661.                 
  1662.             myErr := CreateOffsetDescriptor(startChar, startOfs);
  1663.                         
  1664.             IF myErr=noErr THEN
  1665.                 IF spotFlag THEN
  1666.                     myErr := CreateObjSpecifier( cSpot,
  1667.                                                                              dMyDoc,
  1668.                                                                              formAbsolutePosition, 
  1669.                                                                              startOfs, 
  1670.                                                                              TRUE, 
  1671.                                                                              selTextObj)
  1672.                 ELSE
  1673.                     BEGIN
  1674.                         { not a spot - must represent as range }
  1675.                         { make obj for start char }
  1676.                             
  1677.                         myErr := AECreateDesc(typeNull, nil , 0, dNull);
  1678.                         
  1679.                         myErr := CreateObjSpecifier(cChar, dNull, formAbsolutePosition, startOfs, FALSE, startObj);
  1680.                         
  1681.                         IF (myErr=noErr) THEN
  1682.                             myErr := CreateOffsetDescriptor(endChar, endOfs);
  1683.                         
  1684.                         IF (myErr=noErr) THEN
  1685.                             myErr := CreateObjSpecifier(cChar, dNull, formAbsolutePosition, endOfs, FALSE, endObj);
  1686.                         
  1687.                         IF (myErr=noErr) THEN
  1688.                             myErr := CreateRangeDescriptor(startObj,
  1689.                                                                                         endObj,
  1690.                                                                                         FALSE,
  1691.                                                                                         rangeDesc);
  1692.                         
  1693.                         if (myErr=noErr) then
  1694.                             BEGIN
  1695.                                 myErr := CreateObjSpecifier(cChar,
  1696.                                                                                         dMyDoc,
  1697.                                                                                         formRange,
  1698.                                                                                         rangeDesc,
  1699.                                                                                         TRUE,
  1700.                                                                                         selTextObj);
  1701.                             
  1702.                             END;
  1703.                             
  1704.                         IF (startObj.dataHandle<>NIL) THEN
  1705.                             myErr := AEDisposeDesc(startObj);
  1706.                             
  1707.                         IF (endObj.dataHandle<>NIL) THEN
  1708.                             myErr := AEDisposeDesc(endObj);
  1709.                             
  1710.                         IF (startOfs.dataHandle<>NIL) THEN
  1711.                             myErr := AEDisposeDesc(startOfs);
  1712.                             
  1713.                         IF (endOfs.dataHandle<>NIL) THEN
  1714.                             myErr := AEDisposeDesc(endOfs);
  1715.                     END;
  1716.          END;
  1717.       
  1718.   MakeSelTextObj := myErr;
  1719. END;    { MakeSelTextObj }
  1720.                                             
  1721. { -----------------------------------------------------------------------
  1722.         Name:             DoSetData
  1723.         Purpose:        Handles the SetData Apple Event, extracting the direct
  1724.                                 object (which says what to set) and the data (what to set
  1725.                                 it to).
  1726.      -----------------------------------------------------------------------*}
  1727.      
  1728. FUNCTION DoSetData(theAppleEvent    : AppleEvent;
  1729.                                      reply                    : AppleEvent;
  1730.                                      handlerRefCon    : LongInt): OSErr;
  1731. VAR 
  1732.     myErr                : OSErr;
  1733.     ignoreErr   : OSErr;
  1734.     myDirObj        : AEDesc;
  1735.     myDataDesc    : AEDesc;
  1736.     
  1737. BEGIN
  1738.   myDataDesc.dataHandle := NIL;
  1739.     myDirObj.dataHandle   := NIL;
  1740.     
  1741.   { pick up the direct object, which is the object whose data is to be set }
  1742.     
  1743.   myErr := AEGetParamDesc(theAppleEvent,
  1744.                                                   keyDirectObject,
  1745.                                                     typeWildCard,
  1746.                                                     myDirObj);
  1747.       
  1748.   { now the data to set it to - typeWildCard means get as is}
  1749.   IF (myErr = noErr) THEN
  1750.       myErr := AEGetParamDesc(theAppleEvent,
  1751.                                                         keyAEData,
  1752.                                                         typeWildCard,
  1753.                                                         myDataDesc);
  1754.   
  1755.   { missing any parameters? }
  1756.   IF (myErr = noErr) THEN
  1757.       myErr := GotRequiredParams(theAppleEvent);
  1758.   
  1759.   { set the data }
  1760.   IF (myErr = noErr) THEN
  1761.         myErr := HandleSetData(myDirObj,myDataDesc);
  1762.         
  1763.     IF (myDataDesc.dataHandle<>NIL) THEN
  1764.         ignoreErr := AEDisposeDesc(myDataDesc);
  1765.         
  1766.     IF (myDirObj.dataHandle<>NIL) THEN
  1767.         ignoreErr := AEDisposeDesc(myDirObj);
  1768.   
  1769.   DoSetData := myErr;
  1770. END;    { DoSetData }
  1771.  
  1772. PROCEDURE StyleTokConst(theStyleItem:StyleItem; VAR thekConst:DescType);
  1773.     BEGIN
  1774.         CASE theStyleItem OF
  1775.           bold      : thekConst := kAEBold;
  1776.       italic    : thekConst := kAEItalic;
  1777.       underline : thekConst := kAEUnderline;
  1778.       outline   : thekConst := kAEOutline;
  1779.             shadow    : thekConst := kAEShadow;
  1780.             condense  : thekConst := kAECondensed;
  1781.             extend    : thekConst := kAEExpanded;
  1782.         END;
  1783.     END; (*StyleTokConst*)
  1784.  
  1785. { -----------------------------------------------------------------------
  1786.         Name:             BuildTypeTextStylesDesc
  1787.         Purpose:        Given on and off styles, BuildTextStylesDesc creates an typeTextStyles 
  1788.                     descriptor record containing a pair of lists, keyAEOnStyles
  1789.                                 and keyAEOffStyles. keyAEOffStyles contains all the styles
  1790.                                 we support that are not in theStyle.
  1791.      -----------------------------------------------------------------------*}
  1792.      
  1793. FUNCTION BuildTypeTextStylesDesc(onStyles,offStyles :Style; VAR resultDesc: AEDesc):OSErr;
  1794.     VAR
  1795.             myErr                  : OSErr;
  1796.             ignoreErr          : OSErr;
  1797.             myStyleItem      : styleItem;
  1798.             styleConst    : DescType;
  1799.             onStylesDesc  : AEDesc;
  1800.             offStylesDesc : AEDesc;
  1801.             dataDesc      : AEDesc;
  1802.             
  1803.     BEGIN
  1804.         onStylesDesc.dataHandle  := NIL;
  1805.         offStylesDesc.dataHandle := NIL;
  1806.         dataDesc.dataHandle  := NIL;
  1807.     
  1808.         myErr := AECreateList(NIL, 0, TRUE, dataDesc);
  1809.         
  1810.         myErr := AECreateList(NIL, 0, FALSE, onStylesDesc);
  1811.         myErr := AECreateList(NIL, 0, FALSE, offStylesDesc);
  1812.         
  1813.         FOR myStyleItem := bold TO extend DO
  1814.             IF (myErr=noErr) THEN
  1815.                 BEGIN
  1816.                     StyleTokConst(myStyleItem, styleConst);
  1817.                     IF (myStyleItem IN onStyles) THEN
  1818.                         myErr := AEPutPtr(onStylesDesc,
  1819.                                                             0,                {add to end of list}
  1820.                                                             typeEnumerated, { text for style name }
  1821.                                                             @styleConst,
  1822.                                                             sizeof(styleConst));
  1823.                                                             
  1824.                     IF (myStyleItem IN offStyles) THEN
  1825.                         myErr := AEPutPtr(offStylesDesc,
  1826.                                                             0,                {add to end of list}
  1827.                                                             typeEnumerated, { text for style name }
  1828.                                                             @styleConst,
  1829.                                                             sizeof(styleConst))
  1830.             END;
  1831.         
  1832.         IF (myErr=noErr) THEN
  1833.             myErr := AEPutKeyDesc(dataDesc, keyAEOnStyles,  onStylesDesc);
  1834.         
  1835.         IF (myErr=noErr) THEN
  1836.             myErr := AEPutKeyDesc(dataDesc, keyAEOffStyles, offStylesDesc);
  1837.         
  1838.         IF (myErr=noErr) THEN
  1839.             myErr := AECoerceDesc(dataDesc, typeTextStyles, resultDesc);
  1840.             
  1841.         IF (onStylesDesc.dataHandle <> NIL) THEN
  1842.             ignoreErr := AEDisposeDesc(onStylesDesc);
  1843.             
  1844.         IF (offStylesDesc.dataHandle <> NIL) THEN
  1845.             ignoreErr := AEDisposeDesc(offStylesDesc);
  1846.             
  1847.         IF (dataDesc.dataHandle <> NIL) THEN
  1848.             ignoreErr := AEDisposeDesc(dataDesc);
  1849.     
  1850.         BuildTypeTextStylesDesc := myErr;
  1851.     END; (* BuildTextStylesDesc *)
  1852.  
  1853. FUNCTION BuildTextStylesDesc(theStyle : Style; VAR resultDesc:AEDesc) : OSErr;
  1854.     VAR
  1855.         myStyleItem : StyleItem;
  1856.         onStyles    : Style;
  1857.         offStyles   : Style;
  1858.                 
  1859.     BEGIN
  1860.         onStyles  := [];
  1861.         offStyles := [];
  1862.         
  1863.         FOR myStyleItem := bold TO extend DO
  1864.             BEGIN
  1865.                 IF (myStyleItem IN theStyle) THEN
  1866.                     onStyles  := onStyles  + [myStyleItem]
  1867.                 ELSE
  1868.                     offStyles := offStyles + [myStyleItem];
  1869.             END;
  1870.     
  1871.         BuildTextStylesDesc := BuildTypeTextStylesDesc(onStyles, offStyles, resultDesc);
  1872.         
  1873.     END; (* BuildTextStylesDesc *)
  1874.         
  1875. FUNCTION BuildStyledTextDesc(theHTE : TEHandle; start:INTEGER; howLong:INTEGER; VAR resultDesc:AEDesc):OSErr;
  1876.     VAR
  1877.         listDesc         : AEDesc;
  1878.         oldSelStart      : INTEGER;
  1879.         oldSelEnd        : INTEGER;
  1880.         myStScrpHandle : StScrpHandle;
  1881.         myErr          : OSErr;
  1882.         ignoreErr      : OSErr;
  1883.         
  1884.     BEGIN
  1885.         listDesc.dataHandle := nil;
  1886.         
  1887.         oldSelStart := theHTE^^.selStart;
  1888.         oldSelEnd   := theHTE^^.selEnd;
  1889.         
  1890.         TESetSelect(start-1, start+howLong-2, theHTE);
  1891.         
  1892.         myErr := AECreateList(nil, 0, TRUE,  listDesc);
  1893.         
  1894.         HLock(Handle(theHTE^^.hText));
  1895.                                                          
  1896.         IF (myErr=noErr) THEN
  1897.             myErr := AEPutKeyPtr(listDesc,
  1898.                                  keyAEText,
  1899.                                                      typeChar,
  1900.                                                      Ptr(ORD4(@theHTE^^.hText^^)+start-1),
  1901.                                                      howLong);
  1902.                                                     
  1903.         HUnlock(Handle(theHTE^^.hText));
  1904.         
  1905.         myStScrpHandle := GetStylScrap(theHTE);
  1906.         
  1907.         IF (myStScrpHandle<>NIL) THEN
  1908.             BEGIN
  1909.                 HLock(Handle(myStScrpHandle));
  1910.                 
  1911.                 IF (myErr=noErr) THEN
  1912.                     myErr := AEPutKeyPtr(listDesc,
  1913.                                          keyAEStyles,
  1914.                                                              typeScrapStyles,
  1915.                                                              Ptr(myStScrpHandle^),
  1916.                                                              GetHandleSize(Handle(myStScrpHandle)));
  1917.                     
  1918.                 HUnlock(Handle(myStScrpHandle));
  1919.             END    
  1920.         ELSE
  1921.             myErr := AEPutKeyPtr(listDesc,
  1922.                                                      keyAEStyles,
  1923.                                                      typeScrapStyles,
  1924.                                                      NIL,
  1925.                                                      0);
  1926.         
  1927.         IF (myErr=noErr) THEN
  1928.             myErr := AECoerceDesc(listDesc, typeStyledText, resultDesc); (* should be typeIntlText *)
  1929.         
  1930.         IF (listDesc.dataHandle <> NIL) THEN
  1931.             ignoreErr := AEDisposeDesc(listDesc);
  1932.         
  1933.         TESetSelect(oldSelStart, oldSelEnd, theHTE);
  1934.         
  1935.         BuildStyledTextDesc := myErr;
  1936.     END;
  1937.     
  1938. { -----------------------------------------------------------------------
  1939.         Name:             GetTextProperty
  1940.         Purpose:        Fills dataDesc with the requested text property.
  1941.      -----------------------------------------------------------------------*}
  1942.      
  1943. FUNCTION GetTextProperty(theTokenDesc:AEDesc; VAR dataDesc: AEDesc):OSErr;
  1944.     VAR theDocument     : DPtr;
  1945.             theHTE                 : TEHandle;
  1946.             fontName           : Str255;
  1947.             theSize                : INTEGER;
  1948.             oldPort         : GrafPtr;
  1949.             theTextStyle    : TextStyle;
  1950.             lineHeight        : INTEGER;
  1951.             fontAscent        : INTEGER;
  1952.             theTextPropToken: textPropToken;
  1953.             newDesc         : AEDesc;
  1954.             myErr           : OSErr;
  1955.             tokenSize       : Longint;
  1956.             
  1957.     BEGIN
  1958.       myErr := AECoerceDesc(theTokenDesc, typeMyTextProp, newDesc);
  1959.         IF (myErr = noErr) THEN
  1960.             BEGIN
  1961.                 GetRawDataFromDescriptor(newDesc,
  1962.                                                                  @theTextPropToken,
  1963.                                                                  SizeOf(theTextPropToken),
  1964.                                                                  tokenSize);
  1965.                 myErr:= AEDisposeDesc(newDesc);                                                 
  1966.             END
  1967.         ELSE
  1968.           BEGIN
  1969.                 GetTextProperty := myErr;
  1970.                 Exit(GetTextProperty);
  1971.           END;
  1972.     
  1973.         (*
  1974.             For each property we build a descriptor to be returned as the reply.
  1975.         *)
  1976.                 
  1977.         theDocument := DPtrFromWindowPtr(theTextPropToken.propertyTextToken.tokenWindow);
  1978.         theHTE             := theDocument^.theText;
  1979.         
  1980.         TEGetStyle (theTextPropToken.propertyTextToken.tokenOffset-1,
  1981.                                 theTextStyle,
  1982.                                 lineHeight,
  1983.                                 fontAscent,
  1984.                                 theHTE);
  1985.                                         
  1986.         IF (theTextPropToken.propertyProperty = pText) THEN
  1987.             BEGIN
  1988.                 myErr := BuildStyledTextDesc( theHTE,
  1989.                                                                             theTextPropToken.propertyTextToken.tokenOffset,
  1990.                                                                             theTextPropToken.propertyTextToken.tokenLength,
  1991.                                                                             dataDesc);
  1992.             END
  1993.         ELSE
  1994.         
  1995.         IF (theTextPropToken.propertyProperty = pFont) THEN
  1996.             BEGIN
  1997.             
  1998.                 GetFontName(theTextStyle.tsFont, fontName);
  1999.                 
  2000.                 myErr := AECreateDesc(typeChar,
  2001.                                                             POINTER(ORD4(@fontName)+1),
  2002.                                                             length(fontName),
  2003.                                                             dataDesc);
  2004.             END
  2005.         ELSE
  2006.             
  2007.         IF (theTextPropToken.propertyProperty = pTextStyles) THEN
  2008.             BEGIN                                            
  2009.                 myErr := BuildTextStylesDesc(theTextStyle.tsFace, dataDesc);
  2010.             END
  2011.         ELSE
  2012.             
  2013.         IF (theTextPropToken.propertyProperty = pPointSize) THEN
  2014.             BEGIN
  2015.                 myErr := CreateOffsetDescriptor(theTextStyle.tsSize, dataDesc);
  2016.             END
  2017.         ELSE
  2018.             
  2019.         IF (theTextPropToken.propertyProperty = pScriptTag) THEN
  2020.             BEGIN
  2021.                 myErr := CreateOffsetDescriptor(smSystemScript, dataDesc);
  2022.             END
  2023.         ELSE
  2024.             
  2025.         IF (theTextPropToken.propertyProperty = pStringWidth) THEN
  2026.             BEGIN
  2027.                 GetPort(oldPort);
  2028.                 SetPort(theTextPropToken.propertyTextToken.tokenWindow);
  2029.                 
  2030.                 HLock(Handle(theHTE^^.hText));
  2031.                 theSize := TextWidth(@theHTE^^.hText,
  2032.                                                          theTextPropToken.propertyTextToken.tokenOffset-1,
  2033.                                                          theTextPropToken.propertyTextToken.tokenLength);
  2034.                 
  2035.                 HUnLock(Handle(theHTE^^.hText));
  2036.                 
  2037.                 SetPort(oldPort);
  2038.                 myErr := CreateOffsetDescriptor(theSize, dataDesc);
  2039.             END
  2040.         ELSE
  2041.         
  2042.         IF (theTextPropToken.propertyProperty = pColor) THEN
  2043.             BEGIN
  2044.                 myErr := AECreateDesc(typeRGBColor,
  2045.                                                             @theTextStyle.tsColor,
  2046.                                                             sizeof(theTextStyle.tsColor),
  2047.                                                             dataDesc);
  2048.             END
  2049.         ELSE
  2050.             myErr := errAEEventNotHandled;
  2051.         
  2052.         GetTextProperty := myErr;
  2053.     END; (*GetTextProperty*)
  2054.     
  2055. { -----------------------------------------------------------------------
  2056.         Name:             GetWindowProperty
  2057.         Purpose:        Fills dataDesc with the requested window property.
  2058.      -----------------------------------------------------------------------*}
  2059.      
  2060. FUNCTION GetWindowProperty(theWPTokenObj:AEDesc ;VAR dataDesc:AEDesc):OSErr;
  2061.     TYPE RectHandle = ^RectPtr;
  2062.              RectPtr    = ^Rect;
  2063.              
  2064.     VAR theErr                   : OSErr;
  2065.             theName                : Str255;
  2066.             theBoolean           : Boolean;
  2067.             theRect              : Rect;
  2068.             thePoint           : Point;
  2069.             winRect                   : Rect;
  2070.             userRect               : Rect;
  2071.             theIndex               : INTEGER;
  2072.             theDocument        : DPtr;
  2073.             theHTE                   : TEHandle;
  2074.             theWindowPropToken : windowPropToken;
  2075.             tokenSize          : Longint;
  2076.             newDesc            : AEDesc;
  2077.     
  2078.     BEGIN
  2079.       theErr := AECoerceDesc(theWPTokenObj,typeMyWindowProp,newDesc);
  2080.         
  2081.         IF (theErr = noErr) THEN
  2082.             BEGIN
  2083.                 GetRawDataFromDescriptor(newDesc,
  2084.                                                                  @theWindowPropToken,
  2085.                                                                  SizeOf(theWindowPropToken),
  2086.                                                                  tokenSize);
  2087.                                                                  
  2088.                 theErr := AEDisposeDesc(newDesc);
  2089.             END
  2090.         ELSE
  2091.           BEGIN
  2092.                 GetWindowProperty := theErr;
  2093.                 Exit(GetWindowProperty);
  2094.             END;
  2095.             
  2096.         IF (theWindowPropToken.tokenProperty = pName) THEN
  2097.             BEGIN
  2098.                 GetWTitle(theWindowPropToken.tokenWindowToken,theName);
  2099.                 theErr := AECreateDesc(typeChar,
  2100.                                                              Ptr(ORD4(@theName)+1),
  2101.                                                              Length(theName),
  2102.                                                              dataDesc);
  2103.             END
  2104.         ELSE
  2105.             
  2106.         IF (theWindowPropToken.tokenProperty = pText) THEN
  2107.             BEGIN
  2108.                 theDocument := DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  2109.                 theHTE      := theDocument^.theText;
  2110.                 
  2111.                 theErr := BuildStyledTextDesc(theHTE,
  2112.                                                                             1,
  2113.                                                                             theHTE^^.teLength,
  2114.                                                                             dataDesc);
  2115.             END
  2116.         ELSE
  2117.             
  2118.         IF (theWindowPropToken.tokenProperty = pBounds) THEN
  2119.             BEGIN
  2120.                 SetPort(theWindowPropToken.tokenWindowToken);
  2121.                 
  2122.                 theRect := WindowPeek(theWindowPropToken.tokenWindowToken)^.strucRgn^^.rgnBBox;
  2123.                 
  2124.                 theErr := AECreateDesc(typeQDRectangle,
  2125.                                                              @theRect,
  2126.                                                              sizeOf(theRect),
  2127.                                                              dataDesc);
  2128.             END
  2129.         ELSE
  2130.             
  2131.         IF (theWindowPropToken.tokenProperty = pPosition) THEN
  2132.             BEGIN
  2133.                 thePoint := WindowPeek(theWindowPropToken.tokenWindowToken)^.strucRgn^^.rgnBBox.topLeft;
  2134.                             
  2135.                 theErr := AECreateDesc(typeQDPoint,
  2136.                                                              @thePoint,
  2137.                                                              sizeOf(thePoint),
  2138.                                                              dataDesc);
  2139.             END
  2140.         ELSE
  2141.                         
  2142.         IF (theWindowPropToken.tokenProperty = pVisible) THEN
  2143.             BEGIN
  2144.                 theBoolean := WindowPeek(theWindowPropToken.tokenWindowToken)^.visible;
  2145.                                 
  2146.                 theErr := AECreateDesc(typeBoolean,
  2147.                                                              @theBoolean,
  2148.                                                              sizeOf(theBoolean),
  2149.                                                              dataDesc);
  2150.             END
  2151.         ELSE
  2152.             
  2153.         IF (theWindowPropToken.tokenProperty = pIsModal) THEN
  2154.             BEGIN
  2155.                 theBoolean := FALSE;
  2156.                                 
  2157.                 theErr := AECreateDesc(typeBoolean,
  2158.                                                              @theBoolean,
  2159.                                                              sizeOf(theBoolean),
  2160.                                                              dataDesc);
  2161.             END
  2162.         ELSE
  2163.             
  2164.         IF (theWindowPropToken.tokenProperty = pShowBorders) THEN
  2165.             BEGIN
  2166.                 theDocument := DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  2167.                 theBoolean  := theDocument^.showBorders;
  2168.                                 
  2169.                 theErr := AECreateDesc(typeBoolean,
  2170.                                                              @theBoolean,
  2171.                                                              sizeOf(theBoolean),
  2172.                                                              dataDesc);
  2173.             END
  2174.         ELSE
  2175.             
  2176.         IF (theWindowPropToken.tokenProperty = pIsZoomed) THEN
  2177.             BEGIN
  2178.                 IF (WindowPeek(theWindowPropToken.tokenWindowToken)^.spareFlag) THEN
  2179.                     BEGIN
  2180.                         SetPort(theWindowPropToken.tokenWindowToken);
  2181.                         
  2182.                         userRect := RectHandle(WindowPeek(thePort)^.dataHandle)^^;
  2183.                         winRect  := thePort^.portRect;
  2184.                         LocalToGlobal(winRect.topLeft);
  2185.                         LocalToGlobal(winRect.botRight);
  2186.                         
  2187.                         theBoolean := NOT EqualRect(userRect,winRect); (* right way?? - ***LATER *)
  2188.                         
  2189.                     END
  2190.                 ELSE
  2191.                     theBoolean := FALSE;
  2192.                                 
  2193.                 theErr := AECreateDesc(typeBoolean,
  2194.                                                              @theBoolean,
  2195.                                                              sizeOf(theBoolean),
  2196.                                                              dataDesc);
  2197.             END
  2198.         ELSE
  2199.             
  2200.         IF (theWindowPropToken.tokenProperty = pIsResizable) OR
  2201.            (theWindowPropToken.tokenProperty = pHasTitleBar) OR
  2202.            (theWindowPropToken.tokenProperty = pHasCloseBox) OR
  2203.            (theWindowPropToken.tokenProperty = pIsZoomable) THEN
  2204.              
  2205.             BEGIN
  2206.                 theBoolean := TRUE;
  2207.                                 
  2208.                 theErr := AECreateDesc(typeBoolean,
  2209.                                                              @theBoolean,
  2210.                                                              sizeOf(theBoolean),
  2211.                                                              dataDesc);
  2212.             END
  2213.         ELSE
  2214.         IF (theWindowPropToken.tokenProperty = pIsFloating) THEN
  2215.              
  2216.             BEGIN
  2217.                 theBoolean := FALSE;
  2218.                                 
  2219.                 theErr := AECreateDesc(typeBoolean,
  2220.                                                              @theBoolean,
  2221.                                                              sizeOf(theBoolean),
  2222.                                                              dataDesc);
  2223.             END
  2224.         ELSE
  2225.             
  2226.         IF (theWindowPropToken.tokenProperty = pIsModified) THEN
  2227.             BEGIN
  2228.                 theDocument := DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  2229.                 
  2230.                 theBoolean  := theDocument^.dirty;
  2231.                 
  2232.                 theErr := AECreateDesc(typeBoolean,
  2233.                                                              @theBoolean,
  2234.                                                              sizeOf(theBoolean),
  2235.                                                              dataDesc);
  2236.             END
  2237.         ELSE
  2238.             
  2239.         IF (theWindowPropToken.tokenProperty = pIndex) THEN
  2240.             BEGIN
  2241.                 theIndex := 0;
  2242.                 IF (theWindowPropToken.tokenWindowToken<>nil) THEN
  2243.                     REPEAT
  2244.                         theIndex := theIndex+1;
  2245.                     UNTIL (theWindowPropToken.tokenWindowToken=GetWindowPtrOfNthWindow(theIndex));
  2246.                                 
  2247.                 theErr := AECreateDesc(typeShortInteger,
  2248.                                                              @theIndex,
  2249.                                                              sizeOf(theIndex),
  2250.                                                              dataDesc);
  2251.             END
  2252.         ELSE
  2253.             
  2254.         IF (theWindowPropToken.tokenProperty = pPageSetup) THEN
  2255.             BEGIN
  2256.                 theDocument := DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  2257.                 
  2258.                 theErr := AECreateDesc(typeTPrint,
  2259.                                                              Ptr(theDocument^.thePrintSetup^),
  2260.                                                              SizeOf(TPrint),
  2261.                                                              dataDesc);
  2262.             END
  2263.         ELSE
  2264.           IF (theWindowPropToken.tokenProperty = pSelection) THEN
  2265.               BEGIN
  2266.                     theDocument := DPtrFromWindowPtr(theWindowPropToken.tokenWindowToken);
  2267.                     
  2268.                     theErr  := MakeSelTextObj(theWindowPropToken.tokenWindowToken,
  2269.                                                                         theDocument^.theText, 
  2270.                                                                         dataDesc);                                                             
  2271.                 END
  2272.             ELSE
  2273.                 theErr := kAEGenericErr;
  2274.             
  2275.         GetWindowProperty := theErr;
  2276.     END; (* GetWindowProperty *)
  2277.     
  2278. {* -----------------------------------------------------------------------
  2279.         Name:             GetApplicationProperty
  2280.         Purpose:        Fills dataDesc with the requested application property.
  2281.      -----------------------------------------------------------------------*}
  2282.      
  2283. FUNCTION GetApplicationProperty(theTokenObj:AEDesc; VAR dataDesc: AEDesc): OSErr;
  2284.     VAR theErr           : OSErr;
  2285.             theName          : Str255;
  2286.             isFront          : Boolean;
  2287.             theApplPropToken : ApplPropToken;
  2288.             newDesc          : AEDesc;
  2289.             tokenSize                 : LongInt;
  2290.     
  2291.     BEGIN
  2292.         theErr := AECoerceDesc(theTokenObj,typeMyApplProp,newDesc);
  2293.         IF (theErr=noErr) THEN
  2294.             BEGIN
  2295.                 GetRawDataFromDescriptor(newDesc,
  2296.                                                                  @theApplPropToken,
  2297.                                                                  SizeOf(theApplPropToken),
  2298.                                                                  tokenSize);
  2299.                                                                  
  2300.                 theErr := AEDisposeDesc(newDesc);
  2301.             END
  2302.         ELSE
  2303.           BEGIN
  2304.             GetApplicationProperty := theErr;
  2305.                 Exit(GetApplicationProperty);            
  2306.             END;
  2307.     
  2308.         IF (theApplPropToken.tokenApplProperty = pName) THEN
  2309.             BEGIN
  2310.                 theName:= '7Edit';
  2311.                 theErr := AECreateDesc(typeChar,
  2312.                                                              Ptr(ORD4(@theName)+1),
  2313.                                                              Length(theName),
  2314.                                                              dataDesc);
  2315.             END
  2316.         ELSE
  2317.         IF (theApplPropToken.tokenApplProperty = pVersion) THEN
  2318.             BEGIN
  2319.                 theName:= '3.0d5';
  2320.                 theErr := AECreateDesc(typeChar,
  2321.                                                              Ptr(ORD4(@theName)+1),
  2322.                                                              Length(theName),
  2323.                                                              dataDesc);
  2324.             END
  2325.         ELSE        
  2326.         IF (theApplPropToken.tokenApplProperty = pIsFrontProcess) THEN
  2327.             BEGIN
  2328.                 isFront := NOT gInBackground;
  2329.                 theErr := AECreateDesc(typeBoolean,
  2330.                                                              @isFront,
  2331.                                                              sizeof(isFront),
  2332.                                                              dataDesc);
  2333.             END
  2334.         ELSE
  2335.             theErr := kAEGenericErr;
  2336.  
  2337.         GetApplicationProperty := theErr;
  2338.     END; (* GetApplicationProperty *)
  2339.     
  2340. (* -----------------------------------------------------------------------
  2341.         Name:             GetMenuProperty
  2342.         Purpose:        Fills dataDesc with the requested menu property.
  2343.      -----------------------------------------------------------------------**)
  2344.      
  2345. FUNCTION GetMenuProperty(VAR theObjToken:AEDesc; VAR dataDesc: AEDesc):OSErr;
  2346.     VAR
  2347.       theErr                   : OSErr;
  2348.         theName                  : Str255;
  2349.         theMenuPropToken    : MenuPropToken;
  2350.         newDesc           : AEDesc;
  2351.         tokenSize         : Size;
  2352.         
  2353.     BEGIN
  2354.         theErr := AECoerceDesc(theObjToken, typeMyMenuProp, newDesc);
  2355.         IF (theErr=noErr) THEN
  2356.             BEGIN
  2357.                 GetRawDataFromDescriptor(newDesc,
  2358.                                                                  Ptr(@theMenuPropToken),
  2359.                                                                  sizeof(theMenuPropToken),
  2360.                                                                  tokenSize);
  2361.                                                                  
  2362.                 theErr := AEDisposeDesc(newDesc);
  2363.             END
  2364.         ELSE
  2365.             BEGIN
  2366.                 GetMenuProperty := theErr;
  2367.                 Exit(GetMenuProperty);
  2368.             END;
  2369.  
  2370.         theErr := kAEGenericErr;
  2371.         
  2372.         IF (theMenuPropToken.theMenuProp = pName) THEN
  2373.             BEGIN
  2374.               theName := theMenuPropToken.theMenuToken.theTokenMenu^^.menuData;
  2375.                 theErr  := AECreateDesc(typeChar,
  2376.                                                               Ptr(@theName[1]),
  2377.                                                               length(theName),
  2378.                                                               dataDesc);
  2379.             END;
  2380.         
  2381.         IF (theMenuPropToken.theMenuProp = pMenuID) THEN
  2382.             BEGIN
  2383.                 theErr := AECreateDesc(typeShortInteger,
  2384.                                                              Ptr(@theMenuPropToken.theMenuToken.theTokenID),
  2385.                                                              sizeof(theMenuPropToken.theMenuToken.theTokenID),
  2386.                                                              dataDesc);
  2387.             END;
  2388.                                 
  2389.         GetMenuProperty:=theErr;
  2390.     END; (* GetMenuProperty *)
  2391.     
  2392. (** -----------------------------------------------------------------------
  2393.         Name:             GetMenuItemProperty
  2394.         Purpose:        Fills dataDesc with the requested menu property.
  2395.      -----------------------------------------------------------------------**)
  2396.      
  2397. FUNCTION GetMenuItemProperty(VAR theObjToken:AEDesc; VAR dataDesc:AEDesc):OSErr;
  2398.     VAR
  2399.       theErr               : OSErr;
  2400.         theName              : Str255;
  2401.         theMenuItemPropToken : MenuItemPropToken;
  2402.         newDesc              : AEDesc;
  2403.         tokenSize            : Size;
  2404.         
  2405.     BEGIN    
  2406.         theErr := AECoerceDesc(theObjToken, typeMyItemProp, newDesc);
  2407.         IF (theErr=noErr) THEN
  2408.             BEGIN
  2409.                 GetRawDataFromDescriptor(newDesc,
  2410.                                                                  Ptr(@theMenuItemPropToken),
  2411.                                                                  sizeof(theMenuItemPropToken),
  2412.                                                                  tokenSize);
  2413.                                                                  
  2414.                 theErr := AEDisposeDesc(newDesc);
  2415.             END
  2416.         ELSE
  2417.           BEGIN
  2418.                 GetMenuItemProperty:=theErr;
  2419.                 Exit(GetMenuItemProperty);
  2420.             END;
  2421.  
  2422.         theErr := kAEGenericErr;
  2423.         
  2424.         IF (theMenuItemPropToken.theItemProp = pName) THEN
  2425.             BEGIN
  2426.               GetItem(theMenuItemPropToken.theItemToken.theMenuToken.theTokenMenu,
  2427.                         theMenuItemPropToken.theItemToken.theTokenItem,
  2428.                                 theName);
  2429.                 theErr  := AECreateDesc(typeChar,
  2430.                                                               Ptr(@theName[1]),
  2431.                                                               length(theName),
  2432.                                                               dataDesc);
  2433.             END;
  2434.         
  2435.         IF (theMenuItemPropToken.theItemProp = pItemNumber) THEN
  2436.             BEGIN
  2437.                 theErr := AECreateDesc(typeShortInteger,
  2438.                                                              Ptr(@theMenuItemPropToken.theItemToken.theTokenItem),
  2439.                                                              sizeof(theMenuItemPropToken.theItemToken.theTokenItem),
  2440.                                                              dataDesc);
  2441.             END;
  2442.                     
  2443.         GetMenuItemProperty := theErr;
  2444.     END; (* GetMenuItemProperty *)
  2445.  
  2446. {* -----------------------------------------------------------------------
  2447.         Name:             HandleGetData
  2448.         Purpose:        Coerces theObj into a token which we understand and
  2449.                                 extracts the data requested in the token and puts it
  2450.                                 into dataDesc.
  2451.      -----------------------------------------------------------------------*}
  2452.      
  2453. FUNCTION HandleGetData(theObj: AEDesc; whatType: DescType; VAR dataDesc: AEDesc): OSErr;
  2454. VAR myErr                            : OSErr;
  2455.         newDesc                        : AEDesc;
  2456.         theTextToken      : textToken;
  2457.         tokenSize         : Size;
  2458.         theDoc                        : DPtr;
  2459.         objTokenDesc      : AEDesc;
  2460.  
  2461. BEGIN    
  2462.   myErr:= errAEWrongDataType;
  2463.     (*
  2464.         Resolve theObj into an AEDesc which will contain something we understand
  2465.         - i.e. One of our internal token types
  2466.     *)
  2467.     
  2468.     myErr:= AEResolve(theObj ,kAEIDoMinimum, objTokenDesc);
  2469.   
  2470.     IF (myErr=noErr) THEN
  2471.     
  2472.     IF (objTokenDesc.descriptorType = typeMyApplProp) THEN
  2473.         myErr := GetApplicationProperty(objTokenDesc, dataDesc)
  2474.     ELSE
  2475.     
  2476.     IF (objTokenDesc.descriptorType = typeMyMenuProp) THEN
  2477.         myErr := GetMenuProperty(objTokenDesc, dataDesc)
  2478.     ELSE
  2479.     
  2480.     IF (objTokenDesc.descriptorType = typeMyItemProp) THEN
  2481.         myErr := GetMenuItemProperty(objTokenDesc, dataDesc)
  2482.     ELSE
  2483.     
  2484.     IF (objTokenDesc.descriptorType = typeMyTextProp) THEN
  2485.         myErr := GetTextProperty(objTokenDesc, dataDesc)
  2486.     ELSE
  2487.     
  2488.     IF (objTokenDesc.descriptorType = typeMyWindowProp) THEN
  2489.         myErr := GetWindowProperty(objTokenDesc, dataDesc)
  2490.     ELSE    
  2491.         IF (objTokenDesc.descriptorType = typeMyText) THEN
  2492.             BEGIN
  2493.                 IF (AECoerceDesc(theObj,typeMyText,newDesc) = noErr) THEN
  2494.                     BEGIN
  2495.                         GetRawDataFromDescriptor(newDesc,
  2496.                                                                          @theTextToken,
  2497.                                                                          SizeOf(theTextToken),
  2498.                                                                          tokenSize);
  2499.                                                                          
  2500.                         myErr := AEDisposeDesc(newDesc);
  2501.                                                                          
  2502.                         theDoc                 := DPtrFromWindowPtr(theTextToken.tokenWindow);
  2503.                         
  2504.                         myErr := BuildStyledTextDesc( theDoc^.theText,
  2505.                                                                                     theTextToken.tokenOffset,
  2506.                                                                                     theTextToken.tokenLength,
  2507.                                                                                     dataDesc);
  2508.                     END;
  2509.             END
  2510.         ELSE
  2511.           myErr := kAEGenericErr;
  2512.         
  2513.     HandleGetData := myErr;
  2514. END;    { HandleGetData }
  2515.  
  2516. {* -----------------------------------------------------------------------
  2517.         Name:             DoGetData
  2518.         Purpose:        Handles the GetData AppleEvent.
  2519.      -----------------------------------------------------------------------*}
  2520.  
  2521. FUNCTION DoGetData(theAppleEvent    : AppleEvent;
  2522.                                      reply                    : AppleEvent;
  2523.                                      handlerRefCon    : LongInt): OSErr;
  2524. VAR 
  2525.     myErr                : OSErr;
  2526.     tempErr     : OSErr;
  2527.     myDirObj        : AEDesc;
  2528.     myDataDesc    : AEDesc;
  2529.     actualSize  : Size;
  2530.     returnedType: DescType;
  2531.     reqType     : DescType;
  2532.     
  2533. BEGIN
  2534.     myDataDesc.dataHandle := nil;
  2535.     myDirObj.dataHandle   := nil;
  2536.   
  2537.   (*
  2538.         extract the direct object, which is the object whose data is to be returned
  2539.     *)
  2540.     
  2541.   myErr := AEGetParamDesc(theAppleEvent,
  2542.                                                   keyDirectObject,
  2543.                                                     typeWildCard,
  2544.                                                     myDirObj);
  2545.       
  2546.   (*
  2547.         now the get the type of data wanted - optional
  2548.     *)
  2549.     
  2550.   tempErr := AEGetParamPtr(    theAppleEvent,
  2551.                                                         keyAERequestedType,
  2552.                                                         typeType,
  2553.                                                         returnedType,
  2554.                                                   @reqType,
  2555.                                                         SizeOf(reqType),
  2556.                                                         actualSize);
  2557.     
  2558.     IF (tempErr<>noErr) THEN
  2559.         reqType := typeChar;
  2560.       
  2561.     IF (myErr = noErr) THEN
  2562.       myErr := GotRequiredParams(theAppleEvent);
  2563.   
  2564.   { get the data }
  2565.   IF (myErr = noErr) THEN
  2566.         myErr := HandleGetData(myDirObj, reqType, myDataDesc);
  2567.         
  2568.   { if they wanted a reply, attach it now }
  2569.   IF (myErr=noErr) THEN
  2570.         IF (reply.descriptorType <> typeNull) THEN
  2571.       myErr := AEPutParamDesc(reply, keyDirectObject, myDataDesc);
  2572.  
  2573.   IF (myDataDesc.dataHandle<>NIL) THEN
  2574.         tempErr := AEDisposeDesc(myDataDesc);
  2575.         
  2576.     IF (myDirObj.dataHandle<>NIL) THEN
  2577.         tempErr := AEDisposeDesc(myDirObj);
  2578.         
  2579.   DoGetData := myErr;
  2580. END;    { DoGetData }
  2581.  
  2582.  
  2583. {* -----------------------------------------------------------------------
  2584.         Name:             DoGetDataSize
  2585.         Purpose:        Handles the GetDataSize AppleEvent.
  2586.      -----------------------------------------------------------------------*}
  2587.  
  2588. FUNCTION DoGetDataSize(theAppleEvent    : AppleEvent;
  2589.                                               reply                    : AppleEvent;
  2590.                                               handlerRefCon    : LongInt): OSErr;
  2591. VAR 
  2592.     myErr                : OSErr;
  2593.     tempErr     : OSErr;
  2594.     myDirObj        : AEDesc;
  2595.     myDataDesc    : AEDesc;
  2596.     actualSize  : Size;
  2597.     returnedType: DescType;
  2598.     reqType     : DescType;
  2599.     dataSize        : longInt;
  2600.     
  2601. BEGIN
  2602.     myDataDesc.dataHandle := nil;
  2603.     myDirObj.dataHandle   := nil;
  2604.   
  2605.   { pick up the direct object, which is the object whose data is to be sized }
  2606.     
  2607.   myErr := AEGetParamDesc(theAppleEvent,
  2608.                                                   keyDirectObject,
  2609.                                                     typeWildCard,
  2610.                                                     myDirObj);
  2611.       
  2612.   { now the get the type wanted - optional}
  2613.     
  2614.   tempErr := AEGetParamPtr(    theAppleEvent,
  2615.                                                         keyAERequestedType,
  2616.                                                         typeType,
  2617.                                                         returnedType,
  2618.                                                   @reqType,
  2619.                                                         SizeOf(reqType),
  2620.                                                         actualSize);
  2621.     
  2622.     IF (tempErr<>noErr) THEN
  2623.         reqType := typeChar;
  2624.       
  2625.     IF (myErr = noErr) THEN
  2626.       myErr := GotRequiredParams(theAppleEvent);
  2627.   
  2628.   { get the data }
  2629.   IF (myErr = noErr) THEN
  2630.         myErr := HandleGetData(myDirObj, reqType, myDataDesc);
  2631.         
  2632.     { evaluate size of data and discard, create desc for size }
  2633.     IF (myErr = noErr) THEN
  2634.         IF (myDataDesc.dataHandle<>NIL) THEN
  2635.             BEGIN
  2636.                 dataSize := GetHandleSize(Handle(myDataDesc.dataHandle));
  2637.                 DisposHandle(Handle(myDataDesc.dataHandle));
  2638.                 myErr := AECreateDesc(typeLongInteger,
  2639.                                                             @dataSize,
  2640.                                                             sizeOf(dataSize),
  2641.                                                             myDataDesc);
  2642.             END;
  2643.         
  2644.         
  2645.   { if they wanted a reply, attach it now }
  2646.     
  2647.   IF (myErr=noErr) THEN
  2648.         IF (reply.descriptorType <> typeNull) THEN
  2649.       myErr := AEPutParamDesc(reply, keyDirectObject, myDataDesc);
  2650.             
  2651.     { discard our copy }
  2652.     
  2653.     IF (myDataDesc.dataHandle<>NIL) THEN
  2654.         tempErr := AEDisposeDesc(myDataDesc);
  2655.  
  2656.     IF (myDirObj.dataHandle<>NIL) THEN
  2657.         tempErr := AEDisposeDesc(myDirObj);
  2658.             
  2659.   DoGetDataSize := myErr;
  2660. END;    { DoGetDataSize }
  2661.  
  2662. {* -----------------------------------------------------------------------
  2663.         Name:             DoNewElement
  2664.         Purpose:        Handles the NewElement AppleEvent.
  2665.      -----------------------------------------------------------------------*}
  2666.  
  2667. FUNCTION DoNewElement(theAppleEvent    : AppleEvent;
  2668.                                             reply                    : AppleEvent; 
  2669.                                             handlerRefCon    : LongInt): OSErr;
  2670.                                             
  2671. {
  2672.     Create New Windows - or error
  2673. }
  2674. VAR 
  2675.     myErr                    :    OSErr;
  2676.     ignoreErr     : OSErr;
  2677.     returnedType    :    DescType;
  2678.     newElemClass    :    DescType;
  2679.     actSize                :    Size;
  2680.     wndwObjSpec        :    AEDesc;
  2681.     theDoc        : DPtr;
  2682.     
  2683. BEGIN    
  2684.     wndwObjSpec.dataHandle := NIL;
  2685.         
  2686.     myErr := AEGetParamPtr(    theAppleEvent,
  2687.                                                     keyAEObjectClass,
  2688.                                                     typeType,
  2689.                                                     returnedType,
  2690.                                                     @newElemClass,
  2691.                                                     SizeOf(newElemClass),
  2692.                                                     actSize);
  2693.             
  2694.   { check for missing required parameters }
  2695.     
  2696.   IF (myErr = noErr) THEN
  2697.         myErr := GotRequiredParams(theAppleEvent);
  2698.   
  2699.   { got all required params }
  2700.   
  2701.   { let's make sure container is the null desc }
  2702.   { and they want a window }
  2703.     
  2704.   IF (newElemClass <> cWindow) THEN
  2705.     myErr := errAEWrongDataType;
  2706.   
  2707.   { let's create a new window }
  2708.     
  2709.     IF (myErr = noErr) THEN
  2710.         theDoc := NewDocument(FALSE);
  2711.   
  2712.     IF (myErr = noErr) THEN
  2713.         IF (theDoc = NIL) THEN
  2714.             myErr := -1700
  2715.         ELSE
  2716.             BEGIN
  2717.                 ShowWindow(theDoc^.theWindow);
  2718.                 theDoc^.dirty := FALSE;
  2719.             
  2720.                 myErr := MakeWindowObj(theDoc^.theWindow,
  2721.                                                              wndwObjSpec);
  2722.             END;
  2723.       
  2724.     IF (myErr = noErr) THEN
  2725.         IF (reply.descriptorType <> typeNull) THEN
  2726.              myErr := AEPutParamDesc(reply,
  2727.                                                              keyDirectObject,
  2728.                                                              wndwObjSpec);
  2729.             
  2730.     IF (wndwObjSpec.dataHandle<>NIL) THEN
  2731.         ignoreErr := AEDisposeDesc(wndwObjSpec);
  2732.         
  2733.   DoNewElement := myErr;
  2734. END;    { DoNewElement }
  2735.  
  2736. {* -----------------------------------------------------------------------
  2737.         Name:             DoIsThereA
  2738.         Purpose:        Handles the IsThereA AppleEvent.
  2739.      -----------------------------------------------------------------------*}
  2740.  
  2741. FUNCTION DoIsThereA(theAppleEvent    : AppleEvent;
  2742.                                         reply                    : AppleEvent; 
  2743.                                         handlerRefCon    : LongInt): OSErr;
  2744.                                             
  2745. {
  2746.     Support check of Windows at first
  2747.     
  2748.     What we do :
  2749.         Get Direct Object
  2750.         Check have all required params
  2751.         Coerce into things we support
  2752.         if we get something back
  2753.             check to see it exists and set reply
  2754.         clean up
  2755.         return
  2756. }
  2757.  
  2758. VAR 
  2759.     myErr                    :    OSErr;
  2760.   ignoreErr     : OSErr;
  2761.     myDirObject        :    AEDesc;
  2762.     windDesc      : AEDesc;
  2763.     dataDesc            :    AEDesc;
  2764.     theWindowToken: WindowToken;
  2765.     tokenSize     : Size;
  2766.     exists        : Boolean;
  2767.     
  2768. BEGIN
  2769.   myErr := noErr;
  2770.     
  2771.     myDirObject.dataHandle := NIL;
  2772.     windDesc.dataHandle    := NIL;
  2773.     dataDesc.dataHandle    := NIL;
  2774.         
  2775.     myErr := AEGetParamDesc(theAppleEvent,
  2776.                                                     keyDirectObject,
  2777.                                                     typeWildCard,
  2778.                                                     myDirObject);
  2779.                 
  2780.   { check for missing required parameters }
  2781.     
  2782.   IF (myErr = noErr) THEN
  2783.         myErr := GotRequiredParams(theAppleEvent);
  2784.   
  2785.   { got all required params }
  2786.   
  2787.   { let's make sure they want to check for a window }
  2788.         
  2789.     exists := FALSE;
  2790.     
  2791.   IF (myErr = noErr) THEN
  2792.         IF (AECoerceDesc(myDirObject,typeMyWndw,windDesc) = noErr) THEN
  2793.             IF (windDesc.descriptorType<>typeNull) THEN
  2794.                 BEGIN
  2795.                     GetRawDataFromDescriptor(windDesc,
  2796.                                                                      @theWindowToken,
  2797.                                                                      SizeOf(theWindowToken),
  2798.                                                                      tokenSize);
  2799.                                                                      
  2800.                     exists := (theWindowToken <> NIL);    
  2801.                 END;
  2802.                 
  2803.   IF (myErr = noErr) THEN
  2804.         myErr := AECreateDesc(typeBoolean,
  2805.                                                     @exists,
  2806.                                                     sizeof(exists),
  2807.                                                     dataDesc);
  2808.  
  2809.       
  2810.   { 
  2811.         if they wanted a reply, which they surely must, 
  2812.       attach the result to it…
  2813.     }
  2814.         
  2815.     IF (myErr = noErr) THEN
  2816.         IF (reply.descriptorType <> typeNull) THEN
  2817.              myErr := AEPutParamDesc(reply,
  2818.                                                              keyDirectObject,
  2819.                                                              dataDesc);
  2820.             
  2821.     IF (dataDesc.dataHandle<>NIL) THEN
  2822.         ignoreErr := AEDisposeDesc(dataDesc);
  2823.         
  2824.     IF (myDirObject.dataHandle<>NIL) THEN
  2825.         ignoreErr := AEDisposeDesc(myDirObject);
  2826.         
  2827.     IF (windDesc.dataHandle<>NIL) THEN
  2828.         ignoreErr := AEDisposeDesc(windDesc);
  2829.         
  2830.   DoIsThereA := myErr;
  2831. END;    { DoIsThereA }
  2832.  
  2833. {* -----------------------------------------------------------------------
  2834.         Name:             DoCloseWindow
  2835.         Purpose:        Handles the Close AppleEvent.
  2836.      -----------------------------------------------------------------------*}
  2837.  
  2838. FUNCTION DoCloseWindow(    theAppleEvent    : AppleEvent;
  2839.                                                  reply                    : AppleEvent;
  2840.                                                  handlerRefCon    : LongInt): OSErr;
  2841. VAR 
  2842.     myErr                        : OSErr;
  2843.     tempErr             : OSErr;
  2844.     myDirObj                : AEDesc;
  2845.     newDesc             : AEDesc;
  2846.     theWindowToken    : windowToken;
  2847.     tokenSize                 : Size;
  2848.     saveOpt                    : DescType;
  2849.     actSize                    : Size;
  2850.     returnedType        : DescType;
  2851.     theDoc          : DPtr;
  2852.     
  2853. BEGIN
  2854.   myDirObj.dataHandle := nil;
  2855.     
  2856.   { pick up the direct object, which is the object (window) to close }
  2857.     
  2858.   myErr := AEGetParamDesc(theAppleEvent,
  2859.                                                   keyDirectObject,
  2860.                                                     typeWildCard,
  2861.                                                     myDirObj);
  2862.             
  2863.     { pick up optional save param, if any }
  2864.     
  2865.   saveOpt := kAEAsk; { the default }
  2866.     
  2867.   tempErr := AEGetParamPtr(theAppleEvent,
  2868.                                                      keyAESaveOptions,
  2869.                                                      typeEnumerated,
  2870.                                                      returnedType,
  2871.                                                @saveOpt,
  2872.                                                      SizeOf(saveOpt),
  2873.                                                      actSize);
  2874.     IF (myErr = noErr) THEN
  2875.       myErr := GotRequiredParams(theAppleEvent);
  2876.         
  2877.   { get the window to close as a window ptr }
  2878.   IF (myErr = noErr) THEN
  2879.         IF (AECoerceDesc(myDirObj,typeMyWndw,newDesc) = noErr) THEN
  2880.             IF (newDesc.descriptorType<>typeNull) THEN
  2881.                 BEGIN
  2882.                     GetRawDataFromDescriptor(newDesc,
  2883.                                                                      @theWindowToken,
  2884.                                                                      SizeOf(theWindowToken),
  2885.                                                                      tokenSize);
  2886.                                                                      
  2887.                     myErr := AEDisposeDesc(newDesc);
  2888.                     
  2889.                     IF (theWindowToken<>NIL) THEN
  2890.                         BEGIN
  2891.                             myErr:=AESetInteractionAllowed(kAEInteractWithAll); (* Should do this in prefs *)
  2892.                             
  2893.                             (*
  2894.                                 We do some of the close checks here to avoid
  2895.                                 calling AEInteractWithUser
  2896.                             *)
  2897.                             theDoc := DPtrFromWindowPtr(theWindowToken);
  2898.                             
  2899.                             IF (theDoc^.dirty) OR
  2900.                                  (theDoc^.everSaved=false) THEN
  2901.                                 IF (saveOpt <> kAENo) THEN
  2902.                                     BEGIN
  2903.                                         myErr := AEInteractWithUser(kAEDefaultTimeOut,
  2904.                                                                                                 NIL,
  2905.                                                                                                 NIL);
  2906.                                     END;
  2907.                                 
  2908.                             IF myErr=noErr THEN
  2909.                                 myErr := DoClose(theWindowToken, TRUE, saveOpt);
  2910.                         END
  2911.                     ELSE
  2912.                         myErr:=  errAEIllegalIndex;
  2913.                 END;
  2914.                     
  2915.     IF (myDirObj.dataHandle<>NIL) THEN
  2916.         myErr := AEDisposeDesc(myDirObj);
  2917.             
  2918.   DoCloseWindow := myErr;
  2919. END;    { DoCloseWindow }
  2920.  
  2921. {* -----------------------------------------------------------------------
  2922.         Name:             DoSaveWindow
  2923.         Purpose:        Handles the Save AppleEvent.
  2924.      -----------------------------------------------------------------------*}
  2925.  
  2926. FUNCTION DoSaveWindow(theAppleEvent    : AppleEvent;
  2927.                                              reply                    : AppleEvent;
  2928.                                              handlerRefCon    : LongInt): OSErr;
  2929. VAR 
  2930.     myErr                        : OSErr;
  2931.     tempErr             : OSErr;
  2932.     myDirObj                : AEDesc;
  2933.     newDesc             : AEDesc;
  2934.     theWindowToken    : windowToken;
  2935.     tokenSize                 : Size;
  2936.     actSize                    : Size;
  2937.     returnedType        : DescType;
  2938.     theDoc                    : DPtr;
  2939.     destFSSpec            : FSSpec;
  2940.     
  2941. BEGIN    
  2942.     myDirObj.dataHandle := NIL;
  2943.     
  2944.   { pick up the direct object, which is the window to save }
  2945.     
  2946.   myErr := AEGetParamDesc(theAppleEvent,
  2947.                                                   keyDirectObject,
  2948.                                                     typeWildCard,
  2949.                                                     myDirObj);
  2950.     { pick up optional destination param, if any }
  2951.  
  2952.   tempErr := AEGetParamPtr(theAppleEvent,
  2953.                                                      keyAEDestination,
  2954.                                                      typeFSS,
  2955.                                                      returnedType,
  2956.                                                @destFSSpec,
  2957.                                                      SizeOf(destFSSpec),
  2958.                                                      actSize);
  2959.  
  2960.     IF (myErr = noErr) THEN
  2961.       myErr := GotRequiredParams(theAppleEvent);
  2962.         
  2963.   { get the data }
  2964.  
  2965.     myErr := AECoerceDesc(myDirObj,typeMyWndw,newDesc);
  2966.     
  2967.   IF (myErr = noErr) THEN
  2968.         IF (newDesc.descriptorType<>typeNull) THEN
  2969.             BEGIN
  2970.                 GetRawDataFromDescriptor(newDesc,
  2971.                                                                  @theWindowToken,
  2972.                                                                  SizeOf(theWindowToken),
  2973.                                                                  tokenSize);
  2974.                                                                  
  2975.                 myErr := AEDisposeDesc(newDesc);
  2976.                                                                  
  2977.                 IF (theWindowToken<>NIL) THEN
  2978.                     BEGIN                            
  2979.                         theDoc := DPtrFromWindowPtr(theWindowToken);
  2980.                         
  2981.                         IF (theDoc^.everSaved = false) THEN
  2982.                             IF (tempErr<>noErr) THEN
  2983.                                 (* We had no supplied destination and no default either *)
  2984.                                 myErr := kAEGenericErr;
  2985.                         
  2986.                         IF (myErr=noErr) THEN
  2987.                             IF (tempErr=noErr) THEN
  2988.                                 BEGIN (* we were told where *)
  2989.                                     myErr := DoSave(theDoc, destFSSpec);
  2990.                                     
  2991.                                     IF (myErr=noErr) THEN                                        
  2992.                                         AssocAllSections(theDoc);
  2993.                                 END
  2994.                             ELSE
  2995.                                 myErr := SaveUsingTemp(theDoc);
  2996.                     END
  2997.                 ELSE
  2998.                     myErr:=  errAEIllegalIndex;
  2999.             END;
  3000.             
  3001.   DoSaveWindow := myErr;
  3002.     
  3003.     IF (myDirObj.dataHandle<>NIL) THEN
  3004.         myErr := AEDisposeDesc(myDirObj);
  3005.         
  3006. END;    { DoSaveWindow }
  3007.  
  3008. {* -----------------------------------------------------------------------
  3009.         Name:             DoRevertWindow
  3010.         Purpose:        Handles the Revert AppleEvent.
  3011.      -----------------------------------------------------------------------*}
  3012.  
  3013. FUNCTION DoRevertWindow(theAppleEvent    : AppleEvent;
  3014.                                                  reply                    : AppleEvent;
  3015.                                                  handlerRefCon    : LongInt): OSErr;
  3016. VAR 
  3017.     myErr                        : OSErr;
  3018.     ignoreErr             : OSErr;
  3019.     myDirObj                : AEDesc;
  3020.     newDesc             : AEDesc;
  3021.     theWindowToken    : windowToken;
  3022.     tokenSize                 : Size;
  3023.     theDoc                    : DPtr;
  3024.     
  3025. BEGIN    
  3026.     myDirObj.dataHandle := NIL;
  3027.     
  3028.   { pick up the direct object, which is the window to save }
  3029.     
  3030.   myErr := AEGetParamDesc(theAppleEvent,
  3031.                                                   keyDirectObject,
  3032.                                                     typeWildCard,
  3033.                                                     myDirObj);
  3034.  
  3035.     IF (myErr = noErr) THEN
  3036.       myErr := GotRequiredParams(theAppleEvent);
  3037.         
  3038.   { get the window to revert from the direct object }
  3039.     
  3040.     myErr := AECoerceDesc(myDirObj,typeMyWndw,newDesc);
  3041.     
  3042.   IF (myErr = noErr) THEN
  3043.         IF (newDesc.descriptorType<>typeNull) THEN
  3044.             BEGIN
  3045.                 GetRawDataFromDescriptor(newDesc,
  3046.                                                                  @theWindowToken,
  3047.                                                                  SizeOf(theWindowToken),
  3048.                                                                  tokenSize);
  3049.                                                                  
  3050.                 myErr := AEDisposeDesc(newDesc);
  3051.                                                                  
  3052.                 IF (theWindowToken<>NIL) THEN
  3053.                     BEGIN                            
  3054.                         theDoc := DPtrFromWindowPtr(theWindowToken);
  3055.                         
  3056.                         HidePen;
  3057.                         TESetSelect(0, theDoc^.theText^^.teLength, theDoc^.theText);
  3058.                         ShowPen;
  3059.                         TEDelete(theDoc^.theText);
  3060.         
  3061.                         IF (theDoc^.everSaved) THEN
  3062.                             BEGIN
  3063.                                 myErr := GetFileContents(theDoc^.theFSSpec, theDoc);
  3064.                                 IF (myErr = noErr) THEN
  3065.                                     BEGIN
  3066.                                         ResizeWindow(theDoc);
  3067.                                         theDoc^.dirty := FALSE;
  3068.                                     END;
  3069.                             END;
  3070.         
  3071.                         ShowWindow(theDoc^.theWindow); (* <<< Visible already??? *)
  3072.                         DoUpdate(theDoc);
  3073.                     END
  3074.                 ELSE
  3075.                     myErr:=  errAEIllegalIndex;
  3076.             END;
  3077.  
  3078.     IF (myDirObj.dataHandle<>NIL) THEN
  3079.         ignoreErr := AEDisposeDesc(myDirObj);
  3080.             
  3081.   DoRevertWindow := myErr;
  3082. END;    { DoRevertWindow }
  3083.  
  3084. {*-----------------------------------------------------------------------
  3085.         Name:             DoPrintDocuments
  3086.         Purpose:        Print a list of documents (or windows).
  3087. -----------------------------------------------------------------------*}
  3088. FUNCTION DoPrintDocuments( message, reply: AppleEvent; refcon: LONGINT ): OSErr;
  3089. VAR
  3090.         index                        : LONGINT;
  3091.         itemsInList         : LONGINT;
  3092.         keywd                     : AEKeyWord;
  3093.         err                         : OSErr;
  3094.         docList                 : AEDescList;
  3095.         actSize                 : Size;
  3096.         typeCode                : DescType;
  3097.         theFSSpec             : FSSpec;
  3098.         theWindowToken     : WindowToken;
  3099.         forgetErr           : OSErr;
  3100.         talkToUser      : Boolean;
  3101.         
  3102. BEGIN    
  3103.     err := AEGetParamDesc(message, keyDirectObject, typeAEList, docList) ;
  3104.     err := AECountItems( docList, itemsInList) ;
  3105.         
  3106.     FOR index := 1 TO itemsInList DO
  3107.         IF (err = noErr) THEN
  3108.             BEGIN
  3109.                 forgetErr := AEGetNthPtr( docList, index, typeFSS, keywd, typeCode,
  3110.                                                     @theFSSpec, sizeof(theFSSpec), actSize);
  3111.                                                                     
  3112.                 IF (forgetErr = noErr) THEN
  3113.                     BEGIN
  3114.                         IF (err = noErr) THEN
  3115.                             err := IssueAEOpenDoc(theFSSpec);
  3116.                             
  3117.                         IF (err = noErr) THEN
  3118.                             IssuePrintWindow(FrontWindow);
  3119.                             
  3120.                         IF (err = noErr) THEN
  3121.                             IssueCloseCommand(FrontWindow);
  3122.                     END
  3123.                 ELSE
  3124.                     BEGIN (* wasn't a file - was it a window ? *)
  3125.                         err := AEGetNthPtr( docList, index, typeMyWndw, keywd, typeCode,
  3126.                                                   @theWindowToken, sizeof(windowToken), actSize);
  3127.                                                                 
  3128.                         talkToUser := (AEInteractWithUser(kAEDefaultTimeOut, NIL, NIL) = noErr);
  3129.                         
  3130.                         IF (err = noErr) THEN
  3131.                             PrintWindow(DPtrFromWindowPtr(theWindowToken), talkToUser);
  3132.                     END
  3133.             END;
  3134.         
  3135.     IF (docList.dataHandle<>NIL) THEN
  3136.       forgetErr := AEDisposeDesc(docList);
  3137.         
  3138.     DoPrintDocuments := err;
  3139. END; (* DoPrintDocuments *)
  3140.  
  3141. {*-----------------------------------------------------------------------
  3142.         Name:             HandleCreatePub
  3143.         Purpose:        Create a publisher.
  3144. -----------------------------------------------------------------------*}
  3145. FUNCTION HandleCreatePub( theAppleEvent, reply: AppleEvent; refcon: LONGINT ): OSErr;
  3146. VAR
  3147.         myErr                     : OSErr;
  3148.         theFSSpec             : FSSpec;
  3149.         forgetErr           : OsErr;
  3150.         forget2Err           : OsErr;
  3151.         myDirObj                : AEDesc;
  3152.         myFileLoc                : AEDesc;
  3153.         theTextToken       : TextToken;
  3154.         theDoc          : DPtr;
  3155.         newDesc                    : AEDesc;
  3156.         tokenSize                : LongInt;
  3157.         haveFSSpec            : BOOLEAN;
  3158.         
  3159. BEGIN
  3160.     myErr := noErr;
  3161.     
  3162.   forgetErr  := AEGetParamDesc(theAppleEvent,
  3163.                                                              keyDirectObject,
  3164.                                                              typeWildCard,
  3165.                                                              myDirObj);
  3166.     
  3167.     forget2Err := AEGetParamDesc(theAppleEvent,
  3168.                                                              keyAEEditionFileLoc,
  3169.                                                              typeWildCard,
  3170.                                                              myFileLoc);
  3171.                                                              
  3172.     IF (myErr=noErr) THEN
  3173.         myErr := GotRequiredParams(theAppleEvent);
  3174.         
  3175.     IF (forgetErr=noErr) THEN (* Set the selection to the supplied object - if any *)
  3176.         BEGIN
  3177.             forgetErr := AECoerceDesc(myDirObj,typeMyText,newDesc);
  3178.             IF (newDesc.descriptorType<>typeNull) THEN
  3179.                 BEGIN
  3180.                     GetRawDataFromDescriptor(newDesc,
  3181.                                                                      @theTextToken,
  3182.                                                                      SizeOf(theTextToken),
  3183.                                                                      tokenSize);
  3184.                                                                      
  3185.                     theDoc                 := DPtrFromWindowPtr(theTextToken.tokenWindow);
  3186.                     
  3187.                     TESetSelect(theTextToken.tokenOffset-1,
  3188.                                           theTextToken.tokenOffset+
  3189.                                                                 theTextToken.tokenLength-1,
  3190.                                           theDoc^.theText);
  3191.                                             
  3192.                     forgetErr := AEDisposeDesc(newDesc);
  3193.                 END;
  3194.         END
  3195.     ELSE
  3196.         theDoc := DPtrFromWindowPtr(FrontWindow);
  3197.         
  3198.     IF (theDoc=NIL) THEN
  3199.         BEGIN
  3200.             (* Should clean up and exit with error *)
  3201.         END;
  3202.         
  3203.     haveFSSpec := FALSE;
  3204.     
  3205.     IF (forget2Err=noErr) THEN (* Get the Edition Container File *)
  3206.         BEGIN
  3207.             forget2Err := AECoerceDesc(myDirObj,typeFSS,newDesc);
  3208.             IF (newDesc.descriptorType<>typeNull) THEN
  3209.                 BEGIN
  3210.                     GetRawDataFromDescriptor(newDesc,
  3211.                                                                      @theFSSpec,
  3212.                                                                      SizeOf(theFSSpec),
  3213.                                                                      tokenSize);
  3214.                     forget2Err := AEDisposeDesc(newDesc);
  3215.                     haveFSSpec := TRUE;
  3216.                 END;
  3217.         END;
  3218.         
  3219.     IF (NOT haveFSSpec) THEN
  3220.         myErr := GetEditionContainer(theDoc, theFSSpec);
  3221.  
  3222.     IF (myErr = noErr) THEN
  3223.         myErr := PublishText(theDoc, @theFSSpec);
  3224.         
  3225.     IF (myDirObj.dataHandle<>NIL) THEN
  3226.         forgetErr := AEDisposeDesc(myDirObj);
  3227.     
  3228.     IF (myFileLoc.dataHandle<>NIL) THEN
  3229.         forgetErr := AEDisposeDesc(myFileLoc);
  3230.     
  3231.     HandleCreatePub := myErr;
  3232.     
  3233. END; (* HandleCreatePub *)
  3234.  
  3235.  
  3236. FUNCTION MyCountProc(    desiredType        : DescType;
  3237.                                             containerClass: DescType;
  3238.                                             container            : AEDesc;
  3239.                                             VAR result        : LongInt): OSErr; FORWARD;
  3240.                                             
  3241. {* -----------------------------------------------------------------------
  3242.         Name:             HandleNumberOfElements
  3243.         Purpose:        Handles the Number Of Elements AppleEvent.
  3244.      -----------------------------------------------------------------------*}
  3245.  
  3246. FUNCTION HandleNumberOfElements(theAppleEvent    : AppleEvent;
  3247.                                                                 reply                    : AppleEvent;
  3248.                                                                 handlerRefCon    : LongInt): OSErr;
  3249. VAR myErr                    :    OSErr;
  3250.         myDirObj            :    AEDesc;
  3251.         myClass                :    DescType;
  3252.         myCount                :    LongInt;
  3253.         returnedType     : DescType;
  3254.         actSize                : Size;
  3255.         
  3256. BEGIN
  3257.   myErr      := errAEEventNotHandled;
  3258.   myDirObj.dataHandle := NIL;
  3259.   
  3260.   { pick up direct object, which is the container in which things are to be counted }
  3261.   
  3262.     myErr := AEGetParamDesc(theAppleEvent,
  3263.                                                     keyDirectObject,
  3264.                                                     typeWildCard,
  3265.                                                     myDirObj);
  3266.       
  3267.   { now the class of objects to be counted }
  3268.     
  3269.   myErr := AEGetParamPtr(theAppleEvent,
  3270.                                                  keyAEObjectClass,
  3271.                                                  typeType,
  3272.                                                  returnedType,
  3273.                                                  @myClass,
  3274.                                                  SizeOf(myClass),
  3275.                                                  actSize);
  3276.       
  3277.   { missing any parameters? }
  3278.     
  3279.   myErr := GotRequiredParams(theAppleEvent);
  3280.   
  3281.   { now count }
  3282.  
  3283.     IF (myErr = noErr) THEN
  3284.         myErr := MyCountProc(myClass,myDirObj.descriptorType,myDirObj,myCount);
  3285.   
  3286.   { add result to reply }
  3287.  
  3288.     IF (myErr = noErr) THEN
  3289.         IF reply.descriptorType <> typeNull THEN 
  3290.              myErr := AEPutParamPtr(reply,
  3291.                                                             keyDirectObject,
  3292.                                                             typeLongInteger,
  3293.                                                             @myCount,
  3294.                                                             SizeOf(myCount));
  3295.     IF (myErr = noErr) THEN      
  3296.       myErr := AEDisposeDesc(myDirObj);
  3297.   
  3298.   HandleNumberOfElements := myErr;
  3299.     
  3300. END;    { HandleNumberOfElements }
  3301.  
  3302. {* -----------------------------------------------------------------------
  3303.         Name:             HandleShowSelection
  3304.         Purpose:        Handles the Make Selection Visible AppleEvent.
  3305.      -----------------------------------------------------------------------*}
  3306.  
  3307. FUNCTION HandleShowSelection(theAppleEvent    : AppleEvent;
  3308.                                                          reply                    : AppleEvent;
  3309.                                                          handlerRefCon    : LongInt): OSErr;
  3310. VAR myErr                    :    OSErr;
  3311.     ignoreErr     : OSErr;
  3312.         myDirObj            :    AEDesc;
  3313.         newDesc                :    AEDesc;
  3314.         tokenDesc     : AEDesc;
  3315.         actSize                : Size;
  3316.         theWindowToken: WindowToken;
  3317.         theDocument   : DPtr;
  3318.         theHTE        : TEHandle;
  3319.         
  3320. BEGIN
  3321.     myDirObj.dataHandle  := NIL;
  3322.   tokenDesc.dataHandle := NIL;
  3323.   
  3324.   (*
  3325.         pick up direct object, i.e. the window in which to show the selection
  3326.     *)
  3327.         
  3328.     myErr := AEGetParamDesc(theAppleEvent,
  3329.                                                     keyDirectObject,
  3330.                                                     typeWildCard,
  3331.                                                     myDirObj);
  3332.            
  3333.   (*
  3334.         missing any parameters?
  3335.     *)
  3336.     
  3337.   myErr := GotRequiredParams(theAppleEvent);
  3338.   
  3339.   (*
  3340.         convert object to windowToken which we understand
  3341.     *)
  3342.     
  3343.     myErr := AEResolve(myDirObj, kAEIDoMinimum, tokenDesc);
  3344.     
  3345.   IF (myErr = noErr) THEN
  3346.       IF (tokenDesc.descriptorType=typeMyWndw) THEN
  3347.             BEGIN
  3348.                 IF (AECoerceDesc(myDirObj,typeMyWndw,newDesc) = noErr) THEN
  3349.                     BEGIN
  3350.                         GetRawDataFromDescriptor(newDesc,
  3351.                                                                          @theWindowToken,
  3352.                                                                          SizeOf(theWindowToken),
  3353.                                                                          actSize);
  3354.                                                                          
  3355.                         IF (newDesc.dataHandle<>NIL) THEN
  3356.                             myErr := AEDisposeDesc(newDesc);
  3357.                         
  3358.                         IF (myErr=noErr) THEN
  3359.                             IF (theWindowToken<>NIL) THEN                                                         
  3360.                                 ShowSelect(DPtrFromWindowPtr(theWindowToken))
  3361.                             ELSE
  3362.                                 myErr := errAEIllegalIndex;
  3363.                             
  3364.                     END;
  3365.             END
  3366.         ELSE
  3367.             IF (tokenDesc.descriptorType=typeMyText) THEN
  3368.                 BEGIN
  3369.           myErr := SetSelectionOfAppleEventObject(keyDirectObject,
  3370.                                                                                           theAppleEvent,
  3371.                                                                                            theDocument,
  3372.                                                                                           theHTE);
  3373.                     IF (theDocument<>NIL) THEN                                                         
  3374.                         ShowSelect(theDocument)
  3375.                     ELSE
  3376.                         myErr := errAEIllegalIndex;
  3377.                 END
  3378.             ELSE
  3379.                 myErr      := errAEEventNotHandled;
  3380.   
  3381.     IF (myDirObj.dataHandle<>NIL) THEN
  3382.         ignoreErr := AEDisposeDesc(myDirObj);
  3383.         
  3384.     IF (tokenDesc.dataHandle<>NIL) THEN
  3385.         ignoreErr := AEDisposeDesc(tokenDesc);
  3386.         
  3387.   HandleShowSelection := myErr;
  3388.     
  3389. END;    { HandleShowSelection }
  3390.  
  3391. FUNCTION HandleStartRecording(theAppleEvent    : AppleEvent;
  3392.                                                           reply                    : AppleEvent;
  3393.                                                           handlerRefCon    : LongInt): OSErr;        
  3394. VAR myErr : OSErr;
  3395.  
  3396. BEGIN
  3397.   gBigBrother := gBigBrother +1;
  3398.     
  3399.     myErr := GotRequiredParams(theAppleEvent);
  3400.  
  3401.   HandleStartRecording := noErr;
  3402. END;    { HandleStartRecording }
  3403.  
  3404. FUNCTION HandleStopRecording( theAppleEvent    : AppleEvent;
  3405.                                                           reply                    : AppleEvent;
  3406.                                                           handlerRefCon    : LongInt): OSErr;        
  3407. BEGIN
  3408.   gBigBrother := gBigBrother -1;
  3409.   HandleStopRecording := noErr;
  3410. END;    { HandleStopRecording }
  3411.  
  3412. {$AECommandIssuers}
  3413.  
  3414. {*****************************************************************************}
  3415.         Start of section involved in building and sending AppleEvent Objects as/with
  3416.         commands
  3417.  }
  3418.  
  3419. (*
  3420.     Make an AEDesc that describes the selection in the window and text edit
  3421.     record supplied
  3422. *)
  3423.  
  3424. FUNCTION MakeWindowObj( theWindow  : WindowPtr;
  3425.                                                 VAR dMyDoc : AEDesc):OSErr;
  3426.     VAR dNull           :    AEDesc;
  3427.             dDocName        :    AEDesc;
  3428.             windowName    : Str255;
  3429.             myErr       : OSErr;
  3430.             
  3431.     BEGIN
  3432.         GetWTitle(theWindow,windowName);
  3433.         myErr := AECreateDesc(typeChar,Pointer(ORD4(@windowName)+1), length(windowName), dDocName);
  3434.         
  3435.         if (myErr=noErr) THEN
  3436.             myErr := AECreateDesc(typeNull, nil , 0, dNull);
  3437.         
  3438.         if (myErr=noErr) THEN
  3439.             myErr := CreateObjSpecifier(cWindow, dNull, formName, dDocName, TRUE, dMyDoc);
  3440.         
  3441.         MakeWindowObj:= myErr;
  3442.         
  3443.     END; (*MakeWindowObj*)
  3444.     
  3445. FUNCTION MakeTextObj( theWindow         : WindowPtr;
  3446.                                             selStart          : INTEGER; 
  3447.                                             selEnd            : INTEGER; 
  3448.                                             VAR selTextObj    : AEDesc): OSErr;
  3449. VAR 
  3450.     myErr            : OSErr;
  3451.     ignoreErr    : OSErr;
  3452.     dMyDoc    : AEDesc;
  3453.     startOfs    :    AEDesc;
  3454.     endOfs        : AEDesc;
  3455.     startObj    :    AEDesc;
  3456.     endObj        : AEDesc;
  3457.     rangeDesc    : AEDesc;
  3458.     startChar    :    LongInt;
  3459.     endChar        :    LongInt;
  3460.     spotFlag    :    BOOLEAN;
  3461.         
  3462. BEGIN
  3463.   myErr := noErr;
  3464.     
  3465.     IF (theWindow=nil) THEN
  3466.         BEGIN
  3467.             MakeTextObj:=noErr;
  3468.             Exit(MakeTextObj);
  3469.         END;
  3470.         
  3471.     selTextObj.dataHandle := nil;
  3472.     dMyDoc.dataHandle     := nil;
  3473.     startObj.dataHandle   := nil;
  3474.     endObj.dataHandle     := nil;
  3475.     
  3476.   (* 
  3477.         make the window object 
  3478.     *)
  3479.     
  3480.     myErr := MakeWindowObj(theWindow, dMyDoc);
  3481.         
  3482.   IF (myErr=noErr) THEN
  3483.         BEGIN
  3484.             { get the start and end of selection }
  3485.             
  3486.             startChar := selStart+1;    { start counting obj's from 1, not 0 }
  3487.             endChar   := selEnd;
  3488.             spotFlag  := (selStart = selEnd);
  3489.                 
  3490.             myErr := CreateOffsetDescriptor(startChar, startOfs);
  3491.                         
  3492.             IF myErr=noErr THEN
  3493.                 IF spotFlag THEN
  3494.                     myErr := CreateObjSpecifier( cSpot,
  3495.                                                                              dMyDoc,
  3496.                                                                              formAbsolutePosition, 
  3497.                                                                              startOfs, 
  3498.                                                                              TRUE, 
  3499.                                                                              selTextObj)
  3500.                 ELSE
  3501.                     BEGIN
  3502.                         { not a spot - must represent as range }
  3503.                         { make obj for start char }
  3504.                         myErr := CreateObjSpecifier(cChar, dMyDoc, formAbsolutePosition, startOfs, FALSE, startObj);
  3505.                         
  3506.                         IF (myErr=noErr) THEN
  3507.                             myErr := CreateOffsetDescriptor(endChar, endOfs);
  3508.                         
  3509.                         IF (myErr=noErr) THEN
  3510.                             myErr := CreateObjSpecifier(cChar, dMyDoc, formAbsolutePosition, endOfs, FALSE, endObj);
  3511.                         
  3512.                         IF (myErr=noErr) THEN
  3513.                             myErr := CreateRangeDescriptor(startObj,
  3514.                                                                                         endObj,
  3515.                                                                                         FALSE,
  3516.                                                                                         rangeDesc);
  3517.                         
  3518.                         if (myErr=noErr) then
  3519.                             BEGIN
  3520.                                 myErr := CreateObjSpecifier(cChar,
  3521.                                                                                         dMyDoc,
  3522.                                                                                         formRange,
  3523.                                                                                         rangeDesc,
  3524.                                                                                         TRUE,
  3525.                                                                                         selTextObj);
  3526.                             
  3527.                             END;
  3528.                             
  3529.                         IF (startObj.dataHandle<>NIL) THEN
  3530.                             myErr := AEDisposeDesc(startObj);
  3531.                             
  3532.                         IF (endObj.dataHandle<>NIL) THEN
  3533.                             myErr := AEDisposeDesc(endObj);
  3534.                             
  3535.                         IF (startOfs.dataHandle<>NIL) THEN
  3536.                             myErr := AEDisposeDesc(startOfs);
  3537.                             
  3538.                         IF (endOfs.dataHandle<>NIL) THEN
  3539.                             myErr := AEDisposeDesc(endOfs);
  3540.                     END;
  3541.          END;
  3542.       
  3543.   MakeTextObj := myErr;
  3544. END;    { MakeTextObj }
  3545.  
  3546. FUNCTION MakeSelectedTextObj(theWindow               : WindowPtr;
  3547.                                                          theTextEditHandle : TEHandle;
  3548.                                                          VAR selTextObj    : AEDesc) : OSErr;
  3549.     BEGIN
  3550.         MakeSelectedTextObj:=  MakeTextObj(theWindow,
  3551.                                                                              theTextEditHandle^^.selStart,
  3552.                                                                              theTextEditHandle^^.selEnd,
  3553.                                                                              selTextObj);
  3554.                                                 
  3555.     END;    (* MakeSelectedTextObj *)
  3556.  
  3557. TYPE editCommandType = (editCutCommand, editCopyCommand, editPasteCommand, editClearCommand);
  3558.  
  3559. PROCEDURE DoEditCommand(theDocument:DPtr; whatCommand : editCommandType);
  3560. VAR err                          : OSErr;
  3561.         forgetErr                 : OSErr;
  3562.         ourAddress             : AEAddressDesc;
  3563.         editCommandEvent : AppleEvent;
  3564.         ignoreReply          : AppleEvent;
  3565.         ourTextSelObj    : AEDesc;
  3566.         theEventID             : AEEventID;
  3567.         theEventClass    : AEEventClass;
  3568.  
  3569.     BEGIN
  3570.         (*
  3571.                 Initialise
  3572.         *)
  3573.         ourAddress.dataHandle             := nil;
  3574.         ourTextSelObj.dataHandle         := nil;
  3575.         editCommandEvent.dataHandle := nil;
  3576.         ignoreReply.dataHandle             := nil;
  3577.         
  3578.         err := MakeSelfAddress(ourAddress);
  3579.         
  3580.         (*
  3581.             Build an object to represent the current document's selection
  3582.         *)
  3583.         err := MakeSelectedTextObj(theDocument^.theWindow, theDocument^.theText,ourTextSelObj);
  3584.         
  3585.         IF (err=noErr) THEN
  3586.             BEGIN
  3587.                 CASE whatCommand OF
  3588.                     editCutCommand     : BEGIN
  3589.                                                             theEventID    := kAECut;
  3590.                                                             theEventClass := kAEMiscStandards;
  3591.                                                         END;
  3592.                     editCopyCommand    : BEGIN
  3593.                                                             theEventID    := kAECopy;
  3594.                                                             theEventClass := kAEMiscStandards;
  3595.                                                         END;
  3596.                     editPasteCommand: BEGIN
  3597.                                                             theEventID    := kAEPaste;
  3598.                                                             theEventClass := kAEMiscStandards;
  3599.                                                         END;
  3600.                     editClearCommand: BEGIN
  3601.                                                             theEventID    := kAEDelete;
  3602.                                                             theEventClass := kAECoreSuite;
  3603.                                                         END;
  3604.                 END;
  3605.                 
  3606.                 err := AECreateAppleEvent( theEventClass, theEventID, ourAddress, 0, 0, editCommandEvent);
  3607.               
  3608.                 { add parameter }    
  3609.                 IF (err=noErr) THEN
  3610.                     err := AEPutParamDesc(editCommandEvent,keyDirectObject,ourTextSelObj);
  3611.                     
  3612.                 {and now Send the message}
  3613.                 IF (err=noErr) THEN
  3614.                     err := AESend(editCommandEvent,ignoreReply,kAENoReply,KAEHighPriority,10000,nil, nil);
  3615.             END;
  3616.             
  3617.         (*
  3618.             Clean up
  3619.         *)
  3620.         if (ourAddress.dataHandle<>nil) then
  3621.             err := AEDisposeDesc(ourAddress);
  3622.         
  3623.         if (editCommandEvent.dataHandle<>nil) then
  3624.             err := AEDisposeDesc(editCommandEvent);
  3625.         
  3626.         if (ignoreReply.dataHandle<>nil) then
  3627.             err := AEDisposeDesc(ignoreReply);
  3628.             
  3629.         if (ourTextSelObj.dataHandle<>nil) then
  3630.             err := AEDisposeDesc(ourTextSelObj);
  3631.             
  3632.     END; (*DoEditCommand*)
  3633.     
  3634. PROCEDURE IssueCutCommand(theDocument:DPtr);
  3635.     BEGIN            
  3636.         DoEditCommand(theDocument, editCutCommand);
  3637.     END;
  3638.     
  3639. PROCEDURE IssueCopyCommand(theDocument:DPtr);
  3640.     BEGIN
  3641.         DoEditCommand(theDocument, editCopyCommand);
  3642.     END;
  3643.     
  3644. PROCEDURE IssuePasteCommand(theDocument:DPtr);
  3645.     BEGIN
  3646.         DoEditCommand(theDocument, editPasteCommand);    
  3647.     END;
  3648.     
  3649. PROCEDURE IssueClearCommand(theDocument:DPtr);
  3650.     BEGIN
  3651.         DoEditCommand(theDocument, editClearCommand);    
  3652.     END;
  3653.     
  3654. PROCEDURE IssueFontCommand(theDocument:DPtr;theItem:INTEGER);
  3655.     VAR name         : Str255;
  3656.             strDesc      : AEDesc;
  3657.             theAddress : AEAddressDesc;
  3658.             selTextObj : AEDesc;
  3659.             err                 : OSErr;
  3660.             
  3661.     BEGIN
  3662.         err := MakeSelfAddress(theAddress);
  3663.         
  3664.         err := MakeSelectedTextObj(theDocument^.theWindow, theDocument^.theText, selTextObj);
  3665.         
  3666.         GetItem(myMenus[fontM], theItem, name);
  3667.         
  3668.         IF (err=noErr) THEN
  3669.             err := AECreateDesc(typeChar,
  3670.                                                     POINTER(ORD4(@name)+1),
  3671.                                                     Length(name),
  3672.                                                     strDesc);
  3673.                                                 
  3674.         IF (err=noErr) THEN
  3675.             err := SendAESetObjProp(selTextObj, 
  3676.                                                             pFont,
  3677.                                                             strDesc,
  3678.                                                             theAddress);                                                            
  3679.     END; (* IssueFontCommand *)
  3680.     
  3681. (*
  3682.     Window property routines
  3683. *)
  3684.  
  3685. PROCEDURE IssueZoomCommand(whichWindow:WindowPtr; whichPart:INTEGER);
  3686.     VAR zoomBool         : Boolean;
  3687.             zoomDesc         : AEDesc;
  3688.             selfAddr         : AEAddressDesc;
  3689.             frontWinObj : AEDesc;
  3690.             err                    : OSErr;
  3691.     
  3692.     BEGIN
  3693.         err := MakeSelfAddress(selfAddr);
  3694.         
  3695.         err := MakeWindowObj(whichWindow, frontWinObj);
  3696.         
  3697.         zoomBool := (whichPart=inZoomOut);
  3698.     
  3699.         err := AECreateDesc(typeBoolean,
  3700.                                                 @zoomBool,
  3701.                                                 sizeOf(zoomBool),
  3702.                                                 zoomDesc);
  3703.                                                 
  3704.         err := SendAESetObjProp(frontWinObj, 
  3705.                                                         pIsZoomed,
  3706.                                                         zoomDesc,
  3707.                                                         selfAddr);                                                            
  3708.     END; (* IssueZoomCommand *)
  3709.     
  3710. PROCEDURE IssueCloseCommand(whichWindow:WindowPtr);
  3711.     VAR selfAddr                     : AEAddressDesc;
  3712.             frontWinObj             : AEDesc;
  3713.             err                                : OSErr;
  3714.             ignoreErr                 : OSErr;
  3715.             closeCommandEvent : AppleEvent;
  3716.             ignoreReply                : AppleEvent;
  3717.     
  3718.     BEGIN
  3719.         
  3720.         frontWinObj.dataHandle:=NIL;
  3721.         
  3722.         err := MakeSelfAddress(selfAddr);
  3723.         
  3724.         err := MakeWindowObj(whichWindow, frontWinObj);
  3725.                                                             
  3726.         err := AECreateAppleEvent( kAECoreSuite, kAEClose, selfAddr, 0, 0, closeCommandEvent) ;                
  3727.         
  3728.         { add parameter - the window to close }    
  3729.         IF (err=noErr) THEN
  3730.             err := AEPutParamDesc(closeCommandEvent,keyDirectObject,frontWinObj);
  3731.             
  3732.         IF (err=noErr) THEN
  3733.             err := AESend(closeCommandEvent,ignoreReply,kAENoReply+kAEAlwaysInteract,KAEHighPriority,10000,nil, nil);
  3734.     
  3735.         IF (closeCommandEvent.dataHandle<>NIL) THEN
  3736.             ignoreErr := AEDisposeDesc(closeCommandEvent);
  3737.         
  3738.         IF (selfAddr.dataHandle<>NIL) THEN
  3739.             err := AEDisposeDesc(selfAddr);
  3740.             
  3741.         IF (frontWinObj.dataHandle<>NIL) THEN
  3742.             err := AEDisposeDesc(frontWinObj);
  3743.             
  3744.     END; (* IssueCloseCommand *)
  3745.     
  3746. PROCEDURE IssueSizeWindow(whichWindow:WindowPtr; newHSize:INTEGER; newVSize:INTEGER);
  3747.     VAR sizeRect         : Rect;
  3748.             contentRect : Rect;
  3749.             edgeSize    : INTEGER;
  3750.             sizeDesc         : AEDesc;
  3751.             selfAddr         : AEAddressDesc;
  3752.             frontWinObj : AEDesc;
  3753.             err                    : OSErr;
  3754.     
  3755.     BEGIN
  3756.         sizeRect    := WindowPeek(whichWindow)^.strucRgn^^.rgnBBox;
  3757.         contentRect := WindowPeek(whichWindow)^.contRgn^^.rgnBBox;
  3758.         
  3759.         edgeSize := sizeRect.right-sizeRect.left-(contentRect.right-contentRect.left);
  3760.         sizeRect.right := sizeRect.left+newHSize+edgeSize;
  3761.         
  3762.         edgeSize := sizeRect.bottom-sizeRect.top-(contentRect.bottom-contentRect.top);
  3763.         sizeRect.bottom := sizeRect.top+newVSize+edgeSize;
  3764.         
  3765.         err := MakeSelfAddress(selfAddr);
  3766.         
  3767.         err := MakeWindowObj(whichWindow, frontWinObj);
  3768.             
  3769.         IF (err=noErr) THEN
  3770.             err := AECreateDesc(typeQDRectangle,
  3771.                                                     @sizeRect,
  3772.                                                     sizeOf(sizeRect),
  3773.                                                     sizeDesc);
  3774.                                                     
  3775.       IF (err=noErr) THEN
  3776.             err := SendAESetObjProp(frontWinObj, 
  3777.                                                             pBounds,
  3778.                                                             sizeDesc,
  3779.                                                             selfAddr);                                                            
  3780.     END; (*IssueSizeWindow*)
  3781.     
  3782. PROCEDURE IssueMoveWindow(whichWindow:windowPtr; sizeRect:Rect);
  3783.     VAR sizeDesc         : AEDesc;
  3784.             selfAddr         : AEAddressDesc;
  3785.             frontWinObj : AEDesc;
  3786.             err                    : OSErr;
  3787.     
  3788.     BEGIN        
  3789.         err := MakeSelfAddress(selfAddr);
  3790.         
  3791.         err := MakeWindowObj(whichWindow, frontWinObj);
  3792.             
  3793.         IF (err=noErr) THEN
  3794.             err := AECreateDesc(typeQDRectangle,
  3795.                                                     @sizeRect,
  3796.                                                     sizeOf(sizeRect),
  3797.                                                     sizeDesc);
  3798.         IF (err=noErr) THEN
  3799.             err := SendAESetObjProp(frontWinObj, 
  3800.                                                             pBounds,
  3801.                                                             sizeDesc,
  3802.                                                             selfAddr);                                                            
  3803.     END; (*IssueMoveWindow*)
  3804.  
  3805. PROCEDURE IssuePageSetupWindow(whichWindow:windowPtr; thePageSetup:TPrint);
  3806.     VAR sizeDesc         : AEDesc;
  3807.             selfAddr         : AEAddressDesc;
  3808.             frontWinObj : AEDesc;
  3809.             err                    : OSErr;
  3810.     
  3811.     BEGIN        
  3812.         err := MakeSelfAddress(selfAddr);
  3813.         
  3814.         err := MakeWindowObj(whichWindow, frontWinObj);
  3815.         
  3816.         IF (err=noErr) THEN
  3817.             err := AECreateDesc(typeTPrint,
  3818.                                                     @thePageSetup,
  3819.                                                     sizeOf(thePageSetup),
  3820.                                                     sizeDesc);
  3821.       IF (err=noErr) THEN
  3822.             err := SendAESetObjProp(frontWinObj, 
  3823.                                                             pPageSetup,
  3824.                                                             sizeDesc,
  3825.                                                             selfAddr);                                                            
  3826.     END; (*IssuePageSetupWindow*)
  3827.     
  3828. PROCEDURE IssueShowBorders(whichWindow:windowPtr; showBorders:BOOLEAN);
  3829.     VAR sizeDesc         : AEDesc;
  3830.             selfAddr         : AEAddressDesc;
  3831.             frontWinObj : AEDesc;
  3832.             err                    : OSErr;
  3833.     
  3834.     BEGIN        
  3835.         err := MakeSelfAddress(selfAddr);
  3836.         
  3837.         err := MakeWindowObj(whichWindow, frontWinObj);
  3838.             
  3839.         IF (err=noErr) THEN
  3840.             err := AECreateDesc(typeBoolean,
  3841.                                                     @showBorders,
  3842.                                                     sizeOf(showBorders),
  3843.                                                     sizeDesc);
  3844.         IF (err=noErr) THEN
  3845.             err := SendAESetObjProp(frontWinObj, 
  3846.                                                             pShowBorders,
  3847.                                                             sizeDesc,
  3848.                                                             selfAddr);                                                            
  3849.     END; (*IssueShowBorders*)
  3850.     
  3851. PROCEDURE IssuePrintWindow(whichWindow:windowPtr);
  3852.     VAR selfAddr                     : AEAddressDesc;
  3853.             frontWinObj             : AEDesc;
  3854.             err                                : OSErr;
  3855.             ignoreErr                 : OSErr;
  3856.             printCommandEvent : AppleEvent;
  3857.             ignoreReply                : AppleEvent;
  3858.     
  3859.     BEGIN        
  3860.         err := MakeSelfAddress(selfAddr);
  3861.         
  3862.         err := MakeWindowObj(whichWindow, frontWinObj);
  3863.                                                             
  3864.         err := AECreateAppleEvent( kCoreEventClass, kAEPrintDocuments, selfAddr, 0, 0, printCommandEvent) ;                
  3865.  
  3866.         { 
  3867.             add parameter - the window to print
  3868.         }    
  3869.  
  3870.         IF (err=noErr) THEN
  3871.             err := AEPutParamDesc(printCommandEvent,keyDirectObject,frontWinObj);
  3872.             
  3873.         IF (err=noErr) THEN
  3874.             err := AESend(printCommandEvent,ignoreReply,kAENoReply+kAEAlwaysInteract,KAEHighPriority,10000,nil, nil);
  3875.     
  3876.         err := AEDisposeDesc(printCommandEvent);
  3877.         
  3878.         IF (selfAddr.dataHandle<>NIL) THEN
  3879.             err := AEDisposeDesc(selfAddr);
  3880.             
  3881.         IF (frontWinObj.dataHandle<>NIL) THEN
  3882.             err := AEDisposeDesc(frontWinObj);
  3883.     END; (*IssuePrintWindow*)
  3884.     
  3885. FUNCTION IssueAEOpenDoc(myFSSpec: FSSpec):OSErr;
  3886. { send OpenDocs AppleEvent to myself, with a one-element list
  3887.   containing the given file spec
  3888.  
  3889.   NOTES:    the core AEOpenDocs event is defined as taking a list of
  3890.           aliases (not file specs) as its direct parameter.  However,
  3891.             we can send the file spec instead and depend on AppleEvents'
  3892.             automatic coercion.  In fact, we don't really even have to put 
  3893.             in a list; AppleEvents will coerce a descriptor into a 1-element
  3894.             list if called for.  In this routine, though, we'll make the
  3895.             list for demonstration purposes.
  3896. }
  3897.  
  3898. VAR myAppleEvent:    AppleEvent;
  3899.         defReply        : AppleEvent;
  3900.         docList            : AEDescList;
  3901.         selfAddr        : AEAddressDesc;
  3902.         myErr                : OSErr;
  3903.         
  3904. BEGIN
  3905.     myAppleEvent.dataHandle := NIL;
  3906.     docList.dataHandle  := NIL;
  3907.     selfAddr.dataHandle := NIL;
  3908.     defReply.dataHandle := NIL;
  3909.       
  3910.     {
  3911.         Create empty list and add one file spec
  3912.     }
  3913.   myErr := AECreateList(NIL,0,FALSE,docList);
  3914.     
  3915.     IF (myErr=noErr) THEN
  3916.         myErr := AEPutPtr(docList,1,typeFSS,@myFSSpec,SizeOf(myFSSpec));
  3917.         
  3918.     {
  3919.         Create a self address to send it to
  3920.     }
  3921.     IF (myErr=noErr) THEN
  3922.       myErr := MakeSelfAddress(selfAddr);
  3923.         
  3924.     IF (myErr=noErr) THEN
  3925.         myErr := AECreateAppleEvent(kCoreEventClass,
  3926.                                                                 kAEOpenDocuments,
  3927.                                                                 selfAddr,
  3928.                                                                 kAutoGenerateReturnID,
  3929.                                                                 kAnyTransactionID,
  3930.                                                                 myAppleEvent);
  3931.  
  3932.   { 
  3933.         Put Params into our event and send it
  3934.     }
  3935.   IF (myErr = noErr) THEN
  3936.         myErr := AEPutParamDesc(myAppleEvent,
  3937.                                                         keyDirectObject,
  3938.                                                         docList);
  3939.  
  3940.   myErr := AESend(myAppleEvent,
  3941.                                     defReply,
  3942.                                     kAENoReply+kAEAlwaysInteract,
  3943.                                     kAENormalPriority,
  3944.                                     kAEDefaultTimeOut,
  3945.                                     NIL,
  3946.                                     NIL);
  3947.       
  3948.     IssueAEOpenDoc := myErr;
  3949.     
  3950.     IF (selfAddr.dataHandle<>NIL) THEN
  3951.         myErr := AEDisposeDesc(selfAddr);
  3952.         
  3953.     IF (myAppleEvent.dataHandle<>NIL) THEN
  3954.         myErr := AEDisposeDesc(myAppleEvent);
  3955.         
  3956.     IF (docList.dataHandle<>NIL) THEN
  3957.         myErr := AEDisposeDesc(docList);
  3958.         
  3959. END;    { IssueAEOpenDoc }
  3960.  
  3961. PROCEDURE IssueAENewWindow;
  3962.  
  3963. (* 
  3964.     send the New Element event to myself with a null container
  3965. *)
  3966.  
  3967. VAR myAppleEvent    :    AppleEvent;
  3968.         defReply            :    AppleEvent;
  3969.         selfAddr            : AEAddressDesc;
  3970.         myErr                    : OSErr;
  3971.         ignoreErr         : OSErr;
  3972.         elemClass            : DescType;
  3973.         
  3974. BEGIN
  3975.     myAppleEvent.dataHandle := NIL;
  3976.     
  3977.     (*
  3978.         Create the address of us
  3979.     *)
  3980.     
  3981.     myErr := MakeSelfAddress(selfAddr);
  3982.     
  3983.   (*
  3984.         create event 
  3985.     *)
  3986.     
  3987.   myErr := AECreateAppleEvent(kAECoreSuite,
  3988.                                                             kAECreateElement,
  3989.                                                             selfAddr,
  3990.                                                             kAutoGenerateReturnID,
  3991.                                                             kAnyTransactionID,
  3992.                                                             myAppleEvent);  
  3993.   (*
  3994.         attach desired class of new element
  3995.     *)
  3996.     
  3997.     elemClass := cWindow;
  3998.     
  3999.   IF (myErr = noErr) THEN
  4000.       myErr := AEPutParamPtr(myAppleEvent,
  4001.                                                      keyAEObjectClass,
  4002.                                                      typeType,
  4003.                                                      @elemClass,
  4004.                                                      SizeOf(elemClass));
  4005.       
  4006.   (*
  4007.         send the event 
  4008.     *)
  4009.     
  4010.   IF (myErr = noErr) THEN
  4011.         myErr := AESend(myAppleEvent,
  4012.                                         defReply,
  4013.                                         kAENoReply+kAENeverInteract,
  4014.                                         kAENormalPriority,
  4015.                                         kAEDefaultTimeOut,
  4016.                                         NIL,
  4017.                                         NIL);
  4018.   (*
  4019.         Clean up - reply never created so don't throw away
  4020.     *)
  4021.     IF (selfAddr.dataHandle<>NIL) THEN
  4022.         ignoreErr := AEDisposeDesc(selfAddr);
  4023.         
  4024.     IF (myAppleEvent.dataHandle<>NIL) THEN
  4025.         ignoreErr := AEDisposeDesc(myAppleEvent);
  4026.                 
  4027. END;    { IssueAENewWindow }
  4028.  
  4029. FUNCTION IssueSaveCommand(theWindow    : WindowPtr;
  4030.                                                   where     : FSSpecPtr):OSErr;
  4031. (*
  4032.     send an AppleEvent Save Event to myself
  4033. *)
  4034.  
  4035. VAR windowObj            : AEDesc;
  4036.         myAppleEvent    : AppleEvent;
  4037.         defReply            : AppleEvent;
  4038.         myErr                    : OSErr;
  4039.         ignoreErr         : OSErr;
  4040.         selfAddr            : AEAddressDesc;
  4041.         
  4042. BEGIN
  4043.     windowObj.dataHandle := NIL;
  4044.     myAppleEvent.dataHandle := NIL;
  4045.     
  4046.     myErr := MakeWindowObj(theWindow, windowObj);
  4047.             
  4048.     IF (myErr=noErr) THEN
  4049.         myErr := MakeSelfAddress(selfAddr);
  4050.         
  4051.   (*
  4052.         Build event
  4053.     *)
  4054.     
  4055.   IF (myErr = noErr) THEN
  4056.         myErr := AECreateAppleEvent(kAECoreSuite,
  4057.                                                                 kAESave,
  4058.                                                                 selfAddr,
  4059.                                                                 kAutoGenerateReturnID,
  4060.                                                                 kAnyTransactionID,
  4061.                                                                 myAppleEvent);
  4062.       
  4063.   (*
  4064.         say which window
  4065.     *)
  4066.     
  4067.   IF (myErr=noErr) THEN
  4068.         myErr := AEPutParamDesc(myAppleEvent,
  4069.                                                         keyDirectObject,
  4070.                                                         windowObj);
  4071.   
  4072.   (*
  4073.         add optional file param if we need to
  4074.     *)
  4075.     
  4076.   IF (where<>NIL) THEN
  4077.         IF (myErr=noErr) THEN
  4078.             myErr := AEPutParamPtr(myAppleEvent,
  4079.                                                          keyAEDestination,
  4080.                                                          typeFSS,
  4081.                                                          @where^,
  4082.                                                          SizeOf(where^));
  4083.         
  4084.   (*
  4085.         send the event
  4086.     *)
  4087.   IF (myErr=noErr) THEN
  4088.         myErr := AESend(myAppleEvent,
  4089.                                         defReply,
  4090.                                         kAENoReply+kAENeverInteract,
  4091.                                         kAENormalPriority,
  4092.                                         kAEDefaultTimeOut,
  4093.                                         NIL,
  4094.                                         NIL);
  4095.       
  4096.     IssueSaveCommand := myErr;
  4097.     
  4098.     IF (selfAddr.dataHandle<>NIL) THEN
  4099.         myErr := AEDisposeDesc(selfAddr);
  4100.     
  4101.     IF (windowObj.dataHandle<>NIL) THEN
  4102.         myErr := AEDisposeDesc(windowObj);
  4103.         
  4104.     IF (myAppleEvent.dataHandle<>NIL) THEN
  4105.         myErr := AEDisposeDesc(myAppleEvent);
  4106. END;    { IssueSaveCommand }
  4107.  
  4108. FUNCTION IssueRevertCommand(theWindow    : WindowPtr):OSErr;
  4109. (*
  4110.     send an AppleEvent Revert Event to myself
  4111. *)
  4112.  
  4113. VAR windowObj            : AEDesc;
  4114.         myAppleEvent    : AppleEvent;
  4115.         defReply            : AppleEvent;
  4116.         myErr                    : OSErr;
  4117.         selfAddr            : AEAddressDesc;
  4118.         
  4119. BEGIN
  4120.     windowObj.dataHandle := NIL;
  4121.     myAppleEvent.dataHandle := NIL;
  4122.     
  4123.     myErr := MakeWindowObj(theWindow, windowObj);
  4124.             
  4125.     IF (myErr=noErr) THEN
  4126.         myErr := MakeSelfAddress(selfAddr);
  4127.         
  4128.   (*
  4129.         Build event
  4130.     *)
  4131.     
  4132.   IF (myErr = noErr) THEN
  4133.         myErr := AECreateAppleEvent(kAEMiscStandards,
  4134.                                                                 kAERevert,
  4135.                                                                 selfAddr,
  4136.                                                                 kAutoGenerateReturnID,
  4137.                                                                 kAnyTransactionID,
  4138.                                                                 myAppleEvent);
  4139.   (*
  4140.         say which window
  4141.     *)
  4142.     
  4143.   IF (myErr=noErr) THEN
  4144.         myErr := AEPutParamDesc(myAppleEvent,
  4145.                                                         keyDirectObject,
  4146.                                                         windowObj);
  4147.   (*
  4148.         send the event
  4149.     *)
  4150.   IF (myErr=noErr) THEN
  4151.         myErr := AESend(myAppleEvent,
  4152.                                         defReply,
  4153.                                         kAENoReply+kAENeverInteract,
  4154.                                         kAENormalPriority,
  4155.                                         kAEDefaultTimeOut,
  4156.                                         NIL,
  4157.                                         NIL);
  4158.       
  4159.     IssueRevertCommand := myErr;
  4160.     
  4161.     IF (windowObj.dataHandle<>NIL) THEN
  4162.         myErr := AEDisposeDesc(windowObj);
  4163.         
  4164.     IF (myAppleEvent.dataHandle<>NIL) THEN
  4165.         myErr := AEDisposeDesc(myAppleEvent);
  4166.         
  4167.     IF (selfAddr.dataHandle<>NIL) THEN
  4168.         myErr := AEDisposeDesc(selfAddr);
  4169.         
  4170. END;    { IssueRevertCommand }
  4171.  
  4172. (*
  4173.     Name : IssueQuitCommand
  4174.     Purpose : Sends self a Quit AppleEvent
  4175. *)
  4176. FUNCTION IssueQuitCommand:OSErr;
  4177.     VAR
  4178.         myAppleEvent  : AppleEvent;
  4179.         defReply      : AppleEvent;
  4180.         myErr         : OSErr;
  4181.         ignoreErr     : OSErr;
  4182.         selfAddr      : AEAddressDesc;
  4183.         mySaveOpt     : DescType;
  4184.         
  4185.     BEGIN
  4186.         myAppleEvent.dataHandle := nil;
  4187.         selfAddr.dataHandle     := nil;
  4188.                             
  4189.         myErr := MakeSelfAddress(selfAddr);
  4190.             
  4191.         (*
  4192.             Build event
  4193.         *)
  4194.         
  4195.         IF (myErr = noErr) THEN 
  4196.             myErr  := AECreateAppleEvent(kCoreEventClass,
  4197.                                                                      kAEQuitApplication,
  4198.                                                                      selfAddr,
  4199.                                                                      kAutoGenerateReturnID,
  4200.                                                                      kAnyTransactionID,
  4201.                                                                      myAppleEvent);
  4202.         (*
  4203.             say which save option
  4204.         *)
  4205.         
  4206.         mySaveOpt := kAEAsk;
  4207.         
  4208.         IF (myErr = noErr) THEN
  4209.             myErr := AEPutParamPtr(myAppleEvent,
  4210.                                                          keyAESaveOptions,
  4211.                                                          typeEnumerated,
  4212.                                                          Ptr(@mySaveOpt),
  4213.                                                          sizeof(mySaveOpt));
  4214.         (*
  4215.             send the event
  4216.         *)
  4217.         IF (myErr=noErr) THEN 
  4218.             myErr := AESend(myAppleEvent,
  4219.                                             defReply,
  4220.                                             kAENoReply+kAEAlwaysInteract,
  4221.                                             kAENormalPriority,
  4222.                                             kAEDefaultTimeout,
  4223.                                             nil,
  4224.                                             nil);
  4225.                         
  4226.         IF (myAppleEvent.dataHandle<>NIL) THEN 
  4227.             ignoreErr := AEDisposeDesc(myAppleEvent);
  4228.             
  4229.         IF (selfAddr.dataHandle<>NIL) THEN 
  4230.             ignoreErr := AEDisposeDesc(selfAddr);
  4231.             
  4232.         IssueQuitCommand := myErr;
  4233.     END;    (* IssueQuitCommand *)
  4234.  
  4235. (*
  4236.     Name         : IssueCreatePublisher
  4237.     Purpose : Interact with user to get Publisher info
  4238.                         and the IssueAECommand to Publish currect selection
  4239. *)
  4240. PROCEDURE IssueCreatePublisher(whichDoc:DPtr);
  4241.     VAR selfAddr                         : AEAddressDesc;
  4242.             selTextObj                   : AEDesc;
  4243.             err                                    : OSErr;
  4244.             ignoreErr                        : OSErr;
  4245.             publishCommandEvent : AppleEvent;
  4246.             ignoreReply                    : AppleEvent;
  4247.     
  4248.     BEGIN
  4249.       publishCommandEvent.dataHandle := NIL;
  4250.         selfAddr.dataHandle   := NIL;
  4251.         selTextObj.dataHandle := NIL;
  4252.         
  4253.         err := MakeSelfAddress(selfAddr);
  4254.         IF (err=noErr) THEN
  4255.             err := MakeSelectedTextObj(whichDoc^.theWindow, whichDoc^.theText, selTextObj);
  4256.                                                             
  4257.         err := AECreateAppleEvent( kAEMiscStandards, kAECreatePublisher, selfAddr, 0, 0, publishCommandEvent) ;                
  4258.  
  4259.         { 
  4260.             add parameter - the text to publish
  4261.         }    
  4262.  
  4263.         IF (err=noErr) THEN
  4264.             err := AEPutParamDesc(publishCommandEvent,keyDirectObject,selTextObj);
  4265.             
  4266.         IF (err=noErr) THEN
  4267.             err := AESend(publishCommandEvent,ignoreReply,kAENoReply+kAEAlwaysInteract,KAEHighPriority,10000,nil, nil);
  4268.     
  4269.         IF (publishCommandEvent.dataHandle<>NIL) THEN
  4270.             err := AEDisposeDesc(publishCommandEvent);
  4271.         
  4272.         IF (selTextObj.dataHandle<>NIL) THEN
  4273.             err := AEDisposeDesc(selTextObj);
  4274.             
  4275.         IF (selfAddr.dataHandle<>NIL) THEN
  4276.             err := AEDisposeDesc(selfAddr);
  4277.     END; (*IssueCreatePublisher*)
  4278.     
  4279.  
  4280. FUNCTION PoseSizeDialog(VAR whatSize:longint):BOOLEAN;
  4281.  
  4282. CONST kOK                = 1;
  4283.       kCancel            = 2;
  4284.         kOtherSize         = 4;
  4285.             kOutlineItem     = 5;
  4286.  
  4287. VAR savedPort : GrafPtr;
  4288.     aDialog   : DialogPtr;
  4289.       aString   : STR255;
  4290.       theSize   : LONGINT;
  4291.       itemHit   : INTEGER;
  4292.  
  4293. BEGIN
  4294.   GetPort(savedPort);
  4295.   aDialog := GetNewDialog(1004, nil, Pointer(-1));
  4296.   ShowWindow(aDialog);
  4297.   SetPort(aDialog);
  4298.     
  4299.   AdornDefaultButton(aDialog, kOutlineItem);
  4300.    
  4301.   {set the edittext button to contain the right size}
  4302.   NumToString(whatSize, aString);
  4303.   SetText(aDialog, kOtherSize, aString);
  4304.  
  4305.   REPEAT
  4306.     ModalDialog(nil, itemHit);
  4307.   UNTIL ((itemHit = kOK) OR
  4308.                  (itemHit = kCancel));
  4309.   
  4310.   IF itemHit = kOK THEN 
  4311.         RetrieveText(aDialog, KOtherSize, aString);
  4312.  
  4313.   DisposDialog(aDialog);
  4314.   SetPort(savedPort);
  4315.   
  4316.   IF (itemHit = kOK) THEN
  4317.    BEGIN
  4318.        {set the new size of the text}
  4319.      StringToNum(aString, whatSize);
  4320.          IF (whatSize<1) OR
  4321.             (whatSize>2000) THEN
  4322.              whatSize := 12;
  4323.    END;
  4324.     PoseSizeDialog := (itemHit = kOK);
  4325. END;
  4326.     
  4327. PROCEDURE IssueSizeCommand(theDocument:DPtr;theItem:INTEGER);
  4328.     VAR name         : Str255;
  4329.             sizeDesc      : AEDesc;
  4330.             theAddress : AEAddressDesc;
  4331.             err                 : OSErr;
  4332.             selTextObj : AEDesc;
  4333.             
  4334.             (*
  4335.                 Vars to do with menu processing
  4336.             *)
  4337.             lastSize      : INTEGER;
  4338.             upItem        : INTEGER;
  4339.             downItem         : INTEGER;
  4340.             otherItem     : INTEGER;
  4341.             theSize     : longint;
  4342.             theStyle        : TextStyle;
  4343.             lineHeight    : INTEGER;
  4344.             fontAscent    : INTEGER;
  4345.             
  4346.     BEGIN
  4347.         err := MakeSelfAddress(theAddress);
  4348.         
  4349.         err := MakeSelectedTextObj(theDocument^.theWindow, theDocument^.theText, selTextObj);        
  4350.  
  4351.      {check IF the item is on the Size menu}
  4352.      {remembering that we can add and delete items from it}
  4353.      lastSize  := CountMItems(myMenus[sizeM]) - 5;
  4354.      upItem    := lastSize + 2;
  4355.      downItem  := upItem + 1;
  4356.      otherItem := downItem + 2;
  4357.       
  4358.      TEGetStyle(theDocument^.theText^^.selStart, theStyle, lineHeight, fontAscent, theDocument^.theText);
  4359.         
  4360.      GetItem(myMenus[sizeM], theItem, name);
  4361.      
  4362.      IF theItem <= lastSize THEN
  4363.        BEGIN
  4364.          GetItem(myMenus[sizeM], theItem, name);
  4365.        StringToNum(name, theSize);
  4366.        END
  4367.      ELSE
  4368.        IF theItem = upItem THEN 
  4369.          theSize := theStyle.tsSize+1
  4370.          ELSE
  4371.             IF theItem = downItem THEN 
  4372.                  theSize := theStyle.tsSize-1
  4373.                  ELSE 
  4374.                     If (theItem = otherItem) THEN
  4375.                         BEGIN
  4376.                           theSize := theStyle.tsSize;
  4377.                             IF NOT PoseSizeDialog(theSize) THEN
  4378.                                 Exit(IssueSizeCommand);
  4379.                         END;
  4380.         
  4381.         IF (err=noErr) THEN
  4382.             err := CreateOffsetDescriptor(theSize, sizeDesc);
  4383.  
  4384.         IF (err=noErr) THEN
  4385.             err := SendAESetObjProp(selTextObj, 
  4386.                                                             pPointSize,
  4387.                                                             sizeDesc,
  4388.                                                             theAddress);                                                            
  4389.     END; (*IssueSizeCommand*)
  4390.     
  4391. PROCEDURE IssueStyleCommand(theDocument:DPtr;theItem:INTEGER);
  4392.     VAR theFace          : Style;
  4393.             err                     : OSErr;
  4394.             result           : AEDesc;
  4395.             selfAddr         : AEAddressDesc;
  4396.             selTextObj     : AEDesc;
  4397.             theStyle        : TextStyle;
  4398.             lineHeight    : INTEGER;
  4399.             fontAscent    : INTEGER;
  4400.             
  4401.     BEGIN
  4402.         TEGetStyle(theDocument^.theText^^.selStart, theStyle, lineHeight, fontAscent, theDocument^.theText);
  4403.         
  4404.         theFace := [];
  4405.     case theItem of
  4406.             cPlain            : theFace := [];
  4407.             cBold                : theFace := [bold];
  4408.             cItalic            : theFace := [italic];
  4409.             cUnderLine    : theFace := [underline];
  4410.             cOutline        : theFace := [outline];
  4411.             cShadow            : theFace := [shadow];
  4412.             cCondense        : theFace := [condense];
  4413.             cExtEND            : theFace := [extend];
  4414.     END;{of case}
  4415.  
  4416.     IF (theFace = []) THEN
  4417.       err := BuildTypeTextStylesDesc([], [bold,italic,underline,outline,shadow,condense,extend], result)
  4418.     ELSE
  4419.       IF (theFace * theStyle.tsFace)<>[] THEN
  4420.               err := BuildTypeTextStylesDesc([], theFace, result)
  4421.             ELSE
  4422.               err := BuildTypeTextStylesDesc(theFace, [], result);
  4423.         
  4424.         err := MakeSelfAddress(selfAddr);
  4425.         
  4426.         IF (err=noErr) THEN
  4427.             err := MakeSelectedTextObj(theDocument^.theWindow, theDocument^.theText, selTextObj);
  4428.         
  4429.         IF (err=noErr) THEN
  4430.             err := SendAESetObjProp(selTextObj, 
  4431.                                                             pTextStyles,
  4432.                                                             result,
  4433.                                                             selfAddr);
  4434.         
  4435.     END; (* IssueStyleCommand *)
  4436.  
  4437. CONST
  4438.             ETX = $03; (* Enter key on keyboard or keypad *)
  4439.             BS  = $08; (* Backspace key on keyboard       *)
  4440.             HT  = $09; (* Tab key on keyboard             *)
  4441.             CR  = $0D; (* Return key on keyboard          *)
  4442.             ESC = $1B; (* Clear key on keypad             *)
  4443.             FS  = $1C; (* Left arrow key on keypad        *)
  4444.             GS  = $1D; (* Right arrow key on keypad       *)
  4445.             RS  = $1E; (* Up arrow key on keypad          *)
  4446.             US  = $1F; (* Down arrow key on keypad        *)
  4447.  
  4448. FUNCTION IssueSetDataObjToBufferContents(VAR theObj:AEDesc):OSErr;
  4449.     VAR
  4450.       myErr       : OSErr;
  4451.         ignoreErr   : OSErr;
  4452.         theAddress  : AEAddressDesc;
  4453.         myAppleEvent: AppleEvent;
  4454.         defReply    : AppleEvent;
  4455.  
  4456.     BEGIN
  4457.         myErr := MakeSelfAddress(theAddress);
  4458.         
  4459.         (* create event *)
  4460.         
  4461.         IF (myErr=noErr) THEN
  4462.             myErr:= AECreateAppleEvent(kAECoreSuite,
  4463.                                                                  kAESetData,
  4464.                                                                  theAddress,
  4465.                                                                  0,
  4466.                                                                  0,
  4467.                                                                  myAppleEvent);
  4468.             
  4469.         (* add prop obj spec to the event *)
  4470.         
  4471.         if (myErr=noErr) THEN
  4472.             myErr:= AEPutParamDesc(myAppleEvent, keyDirectObject, theObj);
  4473.         
  4474.         (* add prop data to the event *)
  4475.         
  4476.         if (myErr=noErr) THEN
  4477.             myErr:= AEPutParamPtr(myAppleEvent,
  4478.                                                         keyAEData,
  4479.                                                         typeChar,
  4480.                                                         Ptr(gTypingBuffer),
  4481.                                                         gCharsInBuffer);
  4482.                                                         
  4483.         (* send event *)
  4484.         
  4485.         (* only send the event if recording is implemented, otherwise the event will *)
  4486.         (* be sent twice *)
  4487.         
  4488.         if (gRecordingImplemented = TRUE) THEN
  4489.           if (myErr=noErr) THEN
  4490.               myErr:= AESend(myAppleEvent,
  4491.                                          defReply,
  4492.                                          kAENoReply+kAEDontExecute,
  4493.                                          kAENormalPriority,
  4494.                                          kAEDefaultTimeout,
  4495.                                          nil,
  4496.                                          nil);
  4497.  
  4498.         if (theAddress.dataHandle<>NIL) THEN
  4499.             ignoreErr:= AEDisposeDesc(theAddress);
  4500.             
  4501.         IF (myAppleEvent.dataHandle<>NIL) THEN
  4502.             ignoreErr:= AEDisposeDesc(myAppleEvent);
  4503.             
  4504.         IssueSetDataObjToBufferContents := myErr;
  4505.     END;
  4506.  
  4507. PROCEDURE AddKeyToTypingBuffer(theDocument : DPtr;theKey : CHAR);
  4508.     VAR
  4509.         myErr     : OSErr;
  4510.         ignoreErr : OSErr;
  4511.         
  4512.     BEGIN
  4513.         IF ((theKey=CHR(BS)) OR
  4514.             (theKey=CHR(FS)) OR
  4515.                 (theKey=CHR(GS)) OR 
  4516.                 (theKey=CHR(RS)) OR
  4517.                 (theKey=CHR(US))) THEN
  4518.             BEGIN
  4519.                 FlushAndRecordTypingBuffer;
  4520.                 IF (theKey=CHR(BS)) THEN
  4521.                     BEGIN
  4522.                         IF (theDocument^.theText^^.selStart<>theDocument^.theText^^.selEnd) THEN
  4523.                             BEGIN
  4524.                                 myErr:= MakeTextObj(theDocument^.theWindow,
  4525.                                                                         theDocument^.theText^^.selStart,
  4526.                                                                         theDocument^.theText^^.selEnd,
  4527.                                                                         gTypingTargetObject);
  4528.                             END
  4529.                         ELSE
  4530.                             BEGIN
  4531.                                 myErr:= MakeTextObj(theDocument^.theWindow,
  4532.                                                                         theDocument^.theText^^.selStart-1,
  4533.                                                                         theDocument^.theText^^.selStart,
  4534.                                                                         gTypingTargetObject);
  4535.                             END;
  4536.                             
  4537.                     myErr := IssueSetDataObjToBufferContents(gTypingTargetObject);
  4538.                         
  4539.                         ignoreErr := AEDisposeDesc(gTypingTargetObject);
  4540.                         
  4541.                         gTypingTargetObject.dataHandle := nil;
  4542.                     END
  4543.             END
  4544.         ELSE
  4545.             BEGIN
  4546.                 IF (gCharsInBuffer=0) THEN
  4547.                     myErr:= MakeSelectedTextObj(theDocument^.theWindow,
  4548.                                                                             theDocument^.theText,
  4549.                                                                             gTypingTargetObject);
  4550.  
  4551.                 gTypingBuffer^[gCharsInBuffer] := theKey;
  4552.                 gCharsInBuffer := gCharsInBuffer +1;
  4553.             END;
  4554.     END;
  4555.     
  4556. PROCEDURE FlushAndRecordTypingBuffer;
  4557.     VAR
  4558.         myErr     : OSErr;
  4559.         ignoreErr : OSErr;
  4560.         
  4561.   BEGIN
  4562.         IF (gCharsInBuffer <> 0) THEN
  4563.             BEGIN
  4564.                 myErr:= IssueSetDataObjToBufferContents(gTypingTargetObject);
  4565.                 
  4566.                 IF (gTypingTargetObject.dataHandle<>NIL) THEN
  4567.                     ignoreErr:= AEDisposeDesc(gTypingTargetObject);
  4568.             END;
  4569.             
  4570.         gCharsInBuffer:= 0;
  4571.         gTypingTargetObject.dataHandle:= nil;
  4572.     END;
  4573.     
  4574.  
  4575. {*****************************************************************************}
  4576. {
  4577.     Object Accessors
  4578. }
  4579.     
  4580. FUNCTION WindowFromNullAccessor(wantClass            : DescType;
  4581.                                                                 container            : AEDesc;
  4582.                                                             containerClass: DescType;
  4583.                                                                 form                    : DescType; 
  4584.                                                                 selectionData    : AEDesc;
  4585.                                                                 VAR value            : AEDesc;
  4586.                                                                 theRefCon            : LongInt):  OSErr;
  4587.  
  4588. VAR myErr                :    OSErr;
  4589.         actSize            :    Size;
  4590.         nameStr            :    Str255;
  4591.         theWindow        :    WindowToken;
  4592.         index                :    INTEGER;
  4593.         resultDesc     : AEDesc;
  4594.         
  4595. BEGIN
  4596.   myErr := -1708;    { or whatever }
  4597.   value.dataHandle      := nil;
  4598.     resultDesc.dataHandle := nil;
  4599.   
  4600.   { 
  4601.         should only be called with wantClass = cWindow and
  4602.       with containerClass = typeNull or typeMyAppl.
  4603.         Currently accept as either formName or formAbsolutePosition
  4604.     }
  4605.     
  4606.   IF (wantClass <> cWindow) OR 
  4607.        ((containerClass <> typeNull) AND (containerClass <> typeMyAppl)) OR
  4608.        NOT ((form = formName) OR (form = formAbsolutePosition)) THEN
  4609.     BEGIN
  4610.             WindowFromNullAccessor:=errAEWrongDataType;
  4611.             Exit(WindowFromNullAccessor);
  4612.         END;
  4613.     
  4614.   IF (form = formName) THEN
  4615.         BEGIN
  4616.             myErr     := GetPStringFromDescriptor(selectionData, nameStr);
  4617.             theWindow := WindowNameToWindowPtr(@nameStr);
  4618.         END;
  4619.     
  4620.   IF (form = formAbsolutePosition) THEN
  4621.     BEGIN
  4622.         myErr         := GetIntegerFromDescriptor(selectionData, index);
  4623.           theWindow := GetWindowPtrOfNthWindow(index);
  4624.         END;
  4625.       
  4626.     IF (myErr = noErr) THEN
  4627.         myErr := AECreateDesc(typeMyWndw,@theWindow,SizeOf(theWindow),value);
  4628.           
  4629.   WindowFromNullAccessor := myErr;
  4630. END;    { WindowFromNullAccessor }
  4631.  
  4632. FUNCTION ApplicationFromNullAccessor(wantClass            : DescType;
  4633.                                                                          container            : AEDesc;
  4634.                                                                          containerClass : DescType;
  4635.                                                                          form                      : DescType; 
  4636.                                                                          selectionData    : AEDesc;
  4637.                                                                          VAR value            : AEDesc;
  4638.                                                                          theRefCon            : LongInt):  OSErr;
  4639.  
  4640. VAR myErr                :    OSErr;
  4641.         actSize            :    Size;
  4642.         nameStr            :    Str255;
  4643.         theApp          :    appToken;
  4644.         index                :    INTEGER;
  4645.         resultDesc     : AEDesc;
  4646.         
  4647. BEGIN
  4648.   value.dataHandle      := nil;
  4649.     resultDesc.dataHandle := nil;
  4650.   
  4651.   { 
  4652.         should only be called with wantClass = cWindow and
  4653.       with containerClass = typeNull.
  4654.         Currently accept as either formName or formAbsolutePosition
  4655.     }
  4656.     
  4657.   IF (wantClass <> cApplication) OR (containerClass <> typeNull) OR
  4658.        NOT ((form = formName) OR (form = formAbsolutePosition)) THEN
  4659.     BEGIN
  4660.             ApplicationFromNullAccessor:=errAEWrongDataType;
  4661.             Exit(ApplicationFromNullAccessor);
  4662.         END;
  4663.     
  4664.   IF (form = formName) OR (form = formAbsolutePosition) THEN
  4665.         BEGIN
  4666.             (*     Don't care about data how specified - it's this app *)
  4667.             theApp.highLongOfPSN := 0;
  4668.             theApp.lowLongOfPSN  := kCurrentProcess;
  4669.         END;
  4670.       
  4671.     myErr := AECreateDesc(typeMyAppl,@theApp,SizeOf(theApp),value);
  4672.           
  4673.   ApplicationFromNullAccessor := myErr;
  4674. END;    { ApplicationFromNullAccessor}
  4675.  
  4676. PROCEDURE MoveToNonSpace(VAR start:INTEGER; limit:INTEGER; myChars:CharsHandle);
  4677.     (*
  4678.         Treats space,comma, full stop, ; and : as space chars
  4679.     *)
  4680.     BEGIN
  4681.         WHILE (start<=limit) DO
  4682.             IF myChars^^[start] IN [' ',',','.',';',':',CHR(10),CHR(13)] THEN
  4683.                 start := start+1
  4684.             ELSE
  4685.                 Exit(MoveToNonSpace);
  4686.     END;
  4687.     
  4688. PROCEDURE MoveToSpace(VAR start:INTEGER; limit:INTEGER; myChars:CharsHandle);
  4689.     (*
  4690.         Treats space,comma, full stop, ; and : as space chars
  4691.     *)
  4692.     BEGIN
  4693.         WHILE (start<=limit) DO
  4694.             IF NOT (myChars^^[start] IN [' ',',','.',';',':',CHR(10),CHR(13)]) THEN
  4695.                 start := start+1
  4696.             ELSE
  4697.                 Exit(MoveToSpace);
  4698.     END;
  4699.  
  4700. FUNCTION CountWords(inTextHandle:TEHandle; startAt:INTEGER; forHowManyChars:INTEGER):INTEGER;
  4701.     VAR myChars : CharsHandle;
  4702.             start   : INTEGER;
  4703.             limit   : INTEGER;
  4704.             myWords : INTEGER;
  4705.             
  4706.     BEGIN
  4707.         myChars := CharsHandle(inTextHandle^^.hText);
  4708.         limit   := startAt+forHowManyChars-1;
  4709.         start   := startAt;
  4710.         myWords := 0;
  4711.         MoveToNonSpace(start, limit, myChars);
  4712.         WHILE (start<=limit) DO
  4713.             BEGIN
  4714.                 myWords := myWords+1;
  4715.                 MoveToSpace(start, limit, myChars);
  4716.                 MoveToNonSpace(start, limit, myChars);
  4717.             END;
  4718.         CountWords := myWords;
  4719.     END; (* CountWords *)
  4720.     
  4721. PROCEDURE GetNthWordInfo(whichWord         : INTEGER;
  4722.                                                  inTextHandle      : TEHandle;
  4723.                                                  VAR wordStartChar : INTEGER;
  4724.                                                  VAR wordLength    : INTEGER);
  4725.     (*
  4726.         On entry:    wordStartChar is start of char range to count in
  4727.                             wordLength is number of chars to consider
  4728.                             
  4729.         On Exit : wordStartChar is start of requested word
  4730.                             wordLength is number of chars in word
  4731.     *)
  4732.     VAR myChars : CharsHandle;
  4733.             start   : INTEGER;
  4734.             limit   : INTEGER;
  4735.             
  4736.     BEGIN
  4737.         myChars := CharsHandle(inTextHandle^^.hText);
  4738.         limit   := wordStartChar+wordLength-1;
  4739.         start   := wordStartChar;
  4740.         MoveToNonSpace(start, limit, myChars);
  4741.         WHILE (start<=limit) AND (whichWord>0) DO
  4742.             BEGIN
  4743.                 whichWord     := whichWord-1;
  4744.                 wordStartChar := start;
  4745.                 MoveToSpace(start, limit, myChars);
  4746.                 wordLength    := start-wordStartChar;
  4747.                     
  4748.                 MoveToNonSpace(start, limit, myChars);
  4749.             END;
  4750.     END;(* GetNthWordInfo *)
  4751.  
  4752. PROCEDURE GetWordInfo(whichWord         : INTEGER;
  4753.                                             inTextHandle      : TEHandle;
  4754.                                             VAR wordStartChar : INTEGER;
  4755.                                             VAR wordLength    : INTEGER);
  4756.     (*
  4757.         On entry:    wordStartChar is start of char range to count in
  4758.                             wordLength is number of chars to consider
  4759.                             
  4760.         On Exit : wordStartChar is start of requested word
  4761.                             wordLength is number of chars in word
  4762.     *)
  4763.     VAR noOfWords : INTEGER;
  4764.             
  4765.     BEGIN        
  4766.         noOfWords := CountWords(inTextHandle, wordStartChar, wordLength);
  4767.         
  4768.         IF (whichWord<0) THEN
  4769.             whichWord := noOfWords + whichWord +1;
  4770.             
  4771.         IF (whichWord>noOfWords) THEN
  4772.             BEGIN
  4773.                 wordStartChar := wordStartChar+wordLength;
  4774.                 wordLength    := 0;
  4775.             END
  4776.         ELSE
  4777.             GetNthWordInfo(whichWord, inTextHandle, wordStartChar, wordLength);
  4778.     END;
  4779.     
  4780. FUNCTION CountLines(inTextHandle:TEHandle):INTEGER;
  4781.     BEGIN
  4782.         (*
  4783.             CountLines makes use of info in TERec
  4784.         *)
  4785.         CountLines := inTextHandle^^.nLines;
  4786.     END;
  4787.     
  4788. FUNCTION LineOfOffset(theHTE:TEHandle; charOffset:INTEGER):INTEGER;
  4789.     VAR n : INTEGER;
  4790.     
  4791.     BEGIN
  4792.         n := theHTE^^.nLines;
  4793.         
  4794.         WHILE (theHTE^^.lineStarts[n-1]>charOffset) AND
  4795.                     (n>0) DO
  4796.             n := n-1;
  4797.             
  4798.         LineOfOffset := n;
  4799.         
  4800.     END; (*LineOfOffset*)
  4801.  
  4802. PROCEDURE GetLineInfo(whichLine         : INTEGER;
  4803.                                             inTextHandle      : TEHandle;
  4804.                                             VAR lineStartChar : INTEGER;
  4805.                                             VAR lineLength    : INTEGER);
  4806.     VAR noOfLines   : INTEGER;
  4807.             myChars       : CharsHandle;
  4808.             
  4809.             lineOfStart : INTEGER;
  4810.             lineOfEnd   : INTEGER;
  4811.             
  4812.     BEGIN
  4813.         lineOfStart := LineOfOffset(inTextHandle, lineStartChar);
  4814.         lineOfEnd   := LineOfOffset(inTextHandle, lineStartChar+lineLength-1);
  4815.     
  4816.         myChars := CharsHandle(inTextHandle^^.hText);
  4817.         noOfLines := lineOfEnd - lineOfStart +1;
  4818.         
  4819.         IF (whichLine<0) THEN
  4820.             whichLine := noOfLines + whichLine + 1;
  4821.             
  4822.         noOfLines := CountLines(inTextHandle);
  4823.         whichLine := whichLine + lineOfStart - 1; (* convert offset relative to offset absolute *)
  4824.         
  4825.         (* End of addition *)
  4826.         
  4827.         IF (whichLine<=lineOfEnd) THEN
  4828.             BEGIN
  4829.                 lineStartChar := inTextHandle^^.lineStarts[whichLine-1];
  4830.                 IF (whichLine=noOfLines) THEN
  4831.                     lineLength  := inTextHandle^^.teLength
  4832.                 ELSE
  4833.                     lineLength  := inTextHandle^^.lineStarts[whichLine];
  4834.                 
  4835.                 lineLength    := lineLength-lineStartChar;
  4836.                 (*
  4837.                     Don't return CR
  4838.                 *)
  4839.                 IF (myChars^^[ lineStartChar+lineLength-1] = CHR(13)) THEN
  4840.                     lineLength := lineLength-1;
  4841.             END
  4842.         ELSE
  4843.             BEGIN
  4844.                 IF (whichLine<noOfLines) THEN
  4845.                   lineStartChar := inTextHandle^^.lineStarts[whichLine] (* start of whichLine++ *)
  4846.                 ELSE
  4847.                     lineStartChar := inTextHandle^^.teLength;
  4848.                     
  4849.                 lineLength    := 0;
  4850.             END;
  4851.     END; (* GetLineInfo *)
  4852.     
  4853. FUNCTION TextElemFromWndwAccessor(wantClass            : DescType;
  4854.                                                                     container            : AEDesc ;
  4855.                                                                     containerClass: DescType;
  4856.                                                                     form                    : DescType;
  4857.                                                                     selectionData    : AEDesc;
  4858.                                                                     VAR value            : AEDesc;
  4859.                                                                     theRefCon            : LongInt): OSErr;
  4860. VAR 
  4861.     myErr                        :    OSErr;
  4862.     ignoreErr              :    OSErr;
  4863.     theWindow                :    WindowToken;
  4864.     actSize                    :    Size;
  4865.     index                        :    LongInt;
  4866.     theTextToken        : TextToken;
  4867.     selectionRecord    :    AERecord;
  4868.     startText                : TextToken;
  4869.     stopText                :    TextToken;
  4870.     returnedType        : DescType;
  4871.     windDesc        : AEDesc;
  4872.     theHTE                    : TEHandle;
  4873.     theDocument     : DPtr;
  4874.     wordStartChar   : INTEGER;
  4875.     wordLength      : INTEGER;
  4876.     
  4877. BEGIN
  4878.   myErr := -1700;    { or whatever }
  4879.     
  4880.     selectionRecord.dataHandle := NIL;
  4881.   
  4882.   { do some checking for robustness' sake }
  4883.   
  4884.   IF (containerClass <> cWindow) OR
  4885.        ((wantClass <> cChar) AND 
  4886.              (wantClass <> cSpot) AND 
  4887.             (wantClass <> cWord) AND
  4888.             (wantClass <> cLine)    ) OR
  4889.          ((form<>formRange) AND (form<>formAbsolutePosition)) THEN
  4890.     BEGIN
  4891.           TextElemFromWndwAccessor := errAEWrongDataType;
  4892.             Exit(TextElemFromWndwAccessor);
  4893.         END;
  4894.         
  4895.   { let's get the window which contains the text element }
  4896.     
  4897.     myErr := AECoerceDesc(container, typeMyWndw, windDesc);
  4898.     GetRawDataFromDescriptor(    windDesc,
  4899.                                                         @theWindow,
  4900.                                                         sizeOf(theWindow),
  4901.                                                         actSize);
  4902.     myErr := AEDisposeDesc(windDesc);
  4903.                                                         
  4904.     IF (theWindow=NIL) THEN
  4905.         myErr := errAEIllegalIndex
  4906.     ELSE
  4907.         BEGIN
  4908.                                                                                 
  4909.             theTextToken.tokenWindow := theWindow;
  4910.             
  4911.             theDocument := DPtrFromWindowPtr(theTextToken.tokenWindow);
  4912.             theHTE      := theDocument^.theText;
  4913.             
  4914.             IF (form = formAbsolutePosition) THEN
  4915.                 BEGIN
  4916.                     myErr := GetLongIntFromDescriptor(selectionData, index);
  4917.                     
  4918.                     IF (wantClass=cSpot) THEN
  4919.                         BEGIN
  4920.                             IF (index<0) THEN
  4921.                                 theTextToken.tokenOffset := theHTE^^.teLength+index+2 (* Past last char *)
  4922.                             ELSE
  4923.                                 theTextToken.tokenOffset := index;
  4924.                                 
  4925.                             theTextToken.tokenLength := 0;
  4926.                         END;
  4927.                         
  4928.                     IF (wantClass=cChar) THEN
  4929.                         BEGIN
  4930.                             IF (index<0) THEN
  4931.                                 theTextToken.tokenOffset := theHTE^^.teLength+index+1
  4932.                             ELSE
  4933.                                 theTextToken.tokenOffset := index;
  4934.                             
  4935.                             theTextToken.tokenLength := 1;
  4936.                         END;
  4937.                     
  4938.                     IF (wantClass=cWord) THEN
  4939.                         BEGIN
  4940.                             wordStartChar := 0;
  4941.                             wordLength    := theHTE^^.teLength;
  4942.                             GetWordInfo(index, theHTE, wordStartChar, wordLength); (* zero based *)
  4943.                             theTextToken.tokenOffset := wordStartChar+1;
  4944.                             theTextToken.tokenLength := wordLength;
  4945.                         END;
  4946.                         
  4947.                     IF (wantClass=cLine) THEN
  4948.                         BEGIN
  4949.                             wordStartChar := 0;
  4950.                             wordLength    := theHTE^^.teLength;
  4951.                             GetLineInfo(index, theHTE, wordStartChar, wordLength); (* zero based *)
  4952.                             theTextToken.tokenOffset := wordStartChar+1;
  4953.                             theTextToken.tokenLength := wordLength;
  4954.                         END;
  4955.                 END;    { of formAbsolutePosition }
  4956.             
  4957.             IF form = formRange THEN
  4958.                 BEGIN
  4959.                     { coerce the selection data into an AERecord }
  4960.                     
  4961.                      myErr := AECoerceDesc(selectionData,typeAERecord,selectionRecord);
  4962.                     
  4963.                     { get the start object as a text token - 
  4964.                             this will reenter this proc but as formAbsolutePosition via the coercion handler}
  4965.                     
  4966.                     myErr :=  AEGetKeyPtr(selectionRecord,
  4967.                                                                 keyAERangeStart,
  4968.                                                                 typeMyText,
  4969.                                                                 returnedType,
  4970.                                                                 @startText,
  4971.                                                                 SizeOf(startText),
  4972.                                                                 actSize);
  4973.                     
  4974.                     { now do the same for the stop object }
  4975.                     
  4976.                     IF (myErr=noErr) THEN
  4977.                         myErr :=  AEGetKeyPtr(selectionRecord,
  4978.                                                                     keyAERangeStop,
  4979.                                                                     typeMyText,
  4980.                                                                     returnedType,
  4981.                                                                     @stopText,
  4982.                                                                     SizeOf(stopText),
  4983.                                                                     actSize);
  4984.                     
  4985.                     IF (myErr=noErr) THEN
  4986.                         IF (theTextToken.tokenWindow <> stopText.tokenWindow) OR
  4987.                              (theTextToken.tokenWindow <> startText.tokenWindow) THEN
  4988.                             BEGIN
  4989.                                 myErr := errAECorruptData;    { or whatever }
  4990.                             END;
  4991.                         
  4992.                     theTextToken.tokenOffset  := startText.tokenOffset;            
  4993.                     theTextToken.tokenLength  := stopText.tokenOffset +
  4994.                                                                              stopText.tokenLength - 
  4995.                                                                              startText.tokenOffset;
  4996.                                                                              
  4997.                     IF (theTextToken.tokenLength<0) THEN
  4998.                         myErr := errAECorruptData;    { or whatever }
  4999.                                                                          
  5000.                     IF AEDisposeDesc(selectionRecord)=noErr THEN;
  5001.                     
  5002.                 END;    { of formRange }
  5003.         END;
  5004.     
  5005.     { return theTextToken in a descriptor }
  5006.     
  5007.     IF (myErr=noErr) THEN
  5008.         myErr := AECreateDesc(typeMyText,
  5009.                                                     @theTextToken,
  5010.                                                     SizeOf(theTextToken),
  5011.                                                     value);
  5012.             
  5013.   TextElemFromWndwAccessor := myErr;
  5014. END;    { TextElemFromWndwAccessor }
  5015.  
  5016. FUNCTION TextElemFromTextAccessor(wantClass            : DescType;
  5017.                                                                     container            : AEDesc;
  5018.                                                                     containerClass: DescType;
  5019.                                                                     form                    : DescType;
  5020.                                                                     selectionData    : AEDesc;
  5021.                                                                     VAR value            : AEDesc;
  5022.                                                                     theRefCon            : LongInt): OSErr;
  5023. VAR 
  5024.     myErr                        :    OSErr;
  5025.     actSize                    :    Size;
  5026.     index                        :    LongInt;
  5027.     theTextToken        : TextToken;
  5028.     selectionRecord    :    AERecord;
  5029.     startText                : TextToken;
  5030.     stopText                :    TextToken;
  5031.     returnedType        : DescType;
  5032.     textDesc        : AEDesc;
  5033.     theHTE                    : TEHandle;
  5034.     wordStartChar   : INTEGER;
  5035.     wordLength      : INTEGER;
  5036.     theDocument     : DPtr;
  5037.  
  5038. BEGIN
  5039.   myErr := -1700;    { or whatever }
  5040.       
  5041.   { do some checking for robustness' sake }
  5042.     
  5043.   IF ((wantClass <> cChar) AND 
  5044.         (wantClass <> cSpot) AND 
  5045.         (wantClass <> cLine) AND 
  5046.             (wantClass <> cWord)) OR
  5047.        ((form <> formAbsolutePosition) AND (form <> formRange)) THEN
  5048.     BEGIN
  5049.             TextElemFromTextAccessor := errAEWrongDataType;
  5050.             Exit(TextElemFromTextAccessor);
  5051.         END;
  5052.     
  5053.   { let's get the src text }
  5054.     myErr := AECoerceDesc(container, typeMyText, textDesc);
  5055.     GetRawDataFromDescriptor(    textDesc,
  5056.                                                         @theTextToken,
  5057.                                                         sizeOf(theTextToken),
  5058.                                                         actSize);
  5059.                                                         
  5060.     myErr := AEDisposeDesc(textDesc);
  5061.                                                         
  5062.     theDocument := DPtrFromWindowPtr(theTextToken.tokenWindow);
  5063.     theHTE      := theDocument^.theText;
  5064.     
  5065.   IF (form = formAbsolutePosition) THEN
  5066.     BEGIN
  5067.             myErr := GetLongIntFromDescriptor(selectionData, index);
  5068.             
  5069.             IF (wantClass=cSpot) THEN
  5070.               BEGIN
  5071.                     IF (index<0) THEN
  5072.                         theTextToken.tokenOffset := theTextToken.tokenOffset+index + 1 + theTextToken.tokenLength
  5073.                     ELSE
  5074.                         theTextToken.tokenOffset := theTextToken.tokenOffset+index-1;
  5075.                     theTextToken.tokenLength := 0;
  5076.                 END;
  5077.                 
  5078.             IF (wantClass=cChar) THEN
  5079.                 BEGIN
  5080.                     IF (index<0) THEN
  5081.                         theTextToken.tokenOffset := theTextToken.tokenOffset+index + 1+theTextToken.tokenLength
  5082.                     ELSE
  5083.                         theTextToken.tokenOffset := theTextToken.tokenOffset+index-1;
  5084.                     theTextToken.tokenLength := 1;
  5085.                 END;
  5086.                                 
  5087.             IF (wantClass=cWord) THEN
  5088.                 BEGIN
  5089.                     wordStartChar := theTextToken.tokenOffset-1;
  5090.                     wordLength    := theTextToken.tokenLength;
  5091.                     
  5092.                     GetWordInfo(index, theHTE, wordStartChar, wordLength);(*zero based*)
  5093.                     
  5094.                     theTextToken.tokenOffset := wordStartChar+1;
  5095.                     theTextToken.tokenLength := wordLength;
  5096.                 END;
  5097.                 
  5098.             IF (wantClass=cLine) THEN
  5099.                 BEGIN
  5100.                     wordStartChar := theTextToken.tokenOffset-1;
  5101.                     wordLength    := theTextToken.tokenLength;
  5102.                     
  5103.                     GetLineInfo(index, theHTE, wordStartChar, wordLength);
  5104.                     
  5105.                     theTextToken.tokenOffset := wordStartChar+1;
  5106.                     theTextToken.tokenLength := wordLength;
  5107.                 END;
  5108.  
  5109.         END;    { of formAbsolutePosition }
  5110.     
  5111.   
  5112.   IF (form = formRange) THEN
  5113.     BEGIN
  5114.           { coerce the selection data into an AERecord }
  5115.             
  5116.               myErr := AECoerceDesc(selectionData,typeAERecord,selectionRecord);
  5117.           
  5118.           { get the start object as a text token - 
  5119.                     this will reenter this proc but as formAbsolutePosition via the coercion handler}
  5120.           
  5121.             myErr :=  AEGetKeyPtr(selectionRecord,
  5122.                                                         keyAERangeStart,
  5123.                                                         typeMyText,
  5124.                                                         returnedType,
  5125.                                                   @startText,
  5126.                                                         SizeOf(startText),
  5127.                                                         actSize);
  5128.           
  5129.           { now do the same for the stop object }
  5130.             
  5131.             IF (myErr=noErr) THEN
  5132.                 myErr :=  AEGetKeyPtr(selectionRecord,
  5133.                                                             keyAERangeStop,
  5134.                                                             typeMyText,
  5135.                                                             returnedType,
  5136.                                                             @stopText,
  5137.                                                             SizeOf(stopText),
  5138.                                                             actSize);
  5139.           
  5140.             IF (myErr=noErr) THEN
  5141.                 IF (theTextToken.tokenWindow <> stopText.tokenWindow) OR
  5142.                      (theTextToken.tokenWindow <> startText.tokenWindow) THEN
  5143.                     BEGIN
  5144.                         myErr := errAECorruptData;    { or whatever }
  5145.                     END;
  5146.                 
  5147.             theTextToken.tokenOffset  := startText.tokenOffset;            
  5148.           theTextToken.tokenLength  := stopText.tokenOffset +
  5149.                                                                    stopText.tokenLength - 
  5150.                                                                    startText.tokenOffset;
  5151.                                                                  
  5152.             myErr := AEDisposeDesc(selectionRecord);
  5153.         END;    { of formRange }
  5154.         
  5155.     { return theTextToken in a descriptor }
  5156.     
  5157.     myErr := AECreateDesc(typeMyText,
  5158.                                                 @theTextToken,
  5159.                                                 SizeOf(theTextToken),
  5160.                                                 value);
  5161.     
  5162.   TextElemFromTextAccessor := myErr;
  5163. END;    { TextElemFromTextAccessor }
  5164.  
  5165. FUNCTION PropertyFromTextAccessor(wantClass                : DescType;
  5166.                                                                     container                : AEDesc ;
  5167.                                                                     containerClass    : DescType;
  5168.                                                                     form                        : DescType; 
  5169.                                                                     selectionData        : AEDesc;
  5170.                                                                     VAR value                : AEDesc;
  5171.                                                                     theRefCon                : LongInt): OSErr;
  5172. VAR 
  5173.         myErr                    : OSErr;
  5174.         theTextToken    : TextToken;
  5175.         theProperty        : DescType;
  5176.         textDesc        : AEDesc;
  5177.         propDesc      : AEDesc;
  5178.         actualSize    : Size;
  5179.         myTextProp    : TextPropToken;
  5180.         
  5181. BEGIN
  5182.     value.dataHandle    := nil;
  5183.     textDesc.dataHandle := nil;
  5184.     propDesc.dataHandle := nil;
  5185.   
  5186.   IF (wantClass <> cProperty) OR
  5187.        (form <> formPropertyID) THEN
  5188.     BEGIN
  5189.             PropertyFromTextAccessor:=errAEWrongDataType;
  5190.             Exit(PropertyFromTextAccessor);
  5191.         END;
  5192.     
  5193.   { get the text token }
  5194.   myErr := AECoerceDesc(container, typeMyText, textDesc);
  5195.     GetRawDataFromDescriptor(    textDesc,
  5196.                                                         @theTextToken,
  5197.                                                         sizeOf(theTextToken),
  5198.                                                         actualSize);
  5199.           
  5200.   { get the property }
  5201.   myErr := AECoerceDesc(selectionData, typeType, propDesc);
  5202.     GetRawDataFromDescriptor(    propDesc,
  5203.                                                         @theProperty,
  5204.                                                         sizeOf(theProperty),
  5205.                                                         actualSize);
  5206.     (*
  5207.         Combine the two into single token
  5208.     *)
  5209.     myTextProp.propertyTextToken := theTextToken;
  5210.     myTextProp.propertyProperty  := theProperty;
  5211.     
  5212.   myErr := AECreateDesc(typeMyTextProp,
  5213.                                                 @myTextProp,
  5214.                                                 SizeOf(myTextProp),
  5215.                                                 value);
  5216.       
  5217.     IF (textDesc.dataHandle<>nil) THEN
  5218.         DisposHandle(Handle(textDesc.dataHandle));
  5219.         
  5220.     IF (propDesc.dataHandle<>nil) THEN
  5221.         DisposHandle(Handle(propDesc.dataHandle));
  5222.         
  5223.   PropertyFromTextAccessor := myErr;
  5224. END;    { PropertyFromTextAccessor }
  5225.  
  5226. FUNCTION PropertyFromWndwAccessor(wantClass                : DescType;
  5227.                                                                     container                : AEDesc ;
  5228.                                                                     containerClass    : DescType;
  5229.                                                                     form                        : DescType; 
  5230.                                                                     selectionData        : AEDesc;
  5231.                                                                     VAR value                : AEDesc;
  5232.                                                                     theRefCon                : LongInt): OSErr;
  5233. VAR 
  5234.         myErr                    : OSErr;
  5235.         theWindowToken: WindowToken;
  5236.         theProperty        : DescType;
  5237.         windowDesc    : AEDesc;
  5238.         propDesc      : AEDesc;
  5239.         actualSize    : Size;
  5240.         myWindowProp  : WindowPropToken;
  5241.         
  5242. BEGIN
  5243.     value.dataHandle      := nil;
  5244.     windowDesc.dataHandle := nil;
  5245.     propDesc.dataHandle   := nil;
  5246.   
  5247.   IF (wantClass <> cProperty) OR
  5248.        (form <> formPropertyID) THEN
  5249.     BEGIN
  5250.             PropertyFromWndwAccessor:=errAEWrongDataType;
  5251.             Exit(PropertyFromWndwAccessor);
  5252.         END;
  5253.     
  5254.   { get the window token - it's the container }
  5255.     
  5256.   myErr := AECoerceDesc(container, typeMyWndw, windowDesc);
  5257.     GetRawDataFromDescriptor(    windowDesc,
  5258.                                                         @theWindowToken,
  5259.                                                         sizeOf(theWindowToken),
  5260.                                                         actualSize);
  5261.                                                         
  5262.     { Check the window exists }
  5263.     
  5264.   IF (theWindowToken=NIL) THEN
  5265.         myErr := errAEIllegalIndex
  5266.     ELSE
  5267.         BEGIN
  5268.         
  5269.             { get the property - it's in the selection data }
  5270.             
  5271.             myErr := AECoerceDesc(selectionData, typeType, propDesc);
  5272.             GetRawDataFromDescriptor(    propDesc,
  5273.                                                                 @theProperty,
  5274.                                                                 sizeOf(theProperty),
  5275.                                                                 actualSize);
  5276.             (*
  5277.                 Combine the two into single token
  5278.             *)
  5279.             myWindowProp.tokenWindowToken := theWindowToken;
  5280.             myWindowProp.tokenProperty    := theProperty;
  5281.             
  5282.             myErr := AECreateDesc(typeMyWindowProp,
  5283.                                                         @myWindowProp,
  5284.                                                         SizeOf(myWindowProp),
  5285.                                                         value);
  5286.         END;
  5287.       
  5288.     IF (windowDesc.dataHandle<>nil) THEN
  5289.         DisposHandle(Handle(windowDesc.dataHandle));
  5290.         
  5291.     IF (propDesc.dataHandle<>nil) THEN
  5292.         DisposHandle(Handle(propDesc.dataHandle));
  5293.         
  5294.   PropertyFromWndwAccessor := myErr;
  5295. END;    { PropertyFromWndwAccessor }
  5296.  
  5297. FUNCTION PropertyFromApplAccessor(wantClass                : DescType;
  5298.                                                                     container                : AEDesc ;
  5299.                                                                     containerClass    : DescType;
  5300.                                                                     form                        : DescType; 
  5301.                                                                     selectionData        : AEDesc;
  5302.                                                                     VAR value                : AEDesc;
  5303.                                                                     theRefCon                : LongInt): OSErr;
  5304. VAR 
  5305.         myErr                    : OSErr;
  5306.         theApplToken  : AppToken;
  5307.         theProperty        : DescType;
  5308.         applDesc      : AEDesc;
  5309.         propDesc      : AEDesc;
  5310.         actualSize    : Size;
  5311.         myApplProp    : ApplPropToken;
  5312.         
  5313. BEGIN
  5314.     value.dataHandle      := nil;
  5315.     applDesc.dataHandle   := nil;
  5316.     propDesc.dataHandle   := nil;
  5317.   
  5318.   IF (wantClass <> cProperty) OR
  5319.        (form <> formPropertyID) THEN
  5320.     BEGIN
  5321.             PropertyFromApplAccessor:=errAEWrongDataType;
  5322.             Exit(PropertyFromApplAccessor);
  5323.         END;
  5324.     
  5325.   { get the application token - it's the container }
  5326.     
  5327.   myErr := AECoerceDesc(container, typeMyAppl, applDesc);
  5328.     GetRawDataFromDescriptor(    applDesc,
  5329.                                                         @theApplToken,
  5330.                                                         sizeOf(theApplToken),
  5331.                                                         actualSize);
  5332.           
  5333.   { get the property - it's in the selection data }
  5334.     
  5335.   myErr := AECoerceDesc(selectionData, typeType, propDesc);
  5336.     GetRawDataFromDescriptor(    propDesc,
  5337.                                                         @theProperty,
  5338.                                                         sizeOf(theProperty),
  5339.                                                         actualSize);
  5340.     (*
  5341.         Combine the two into single token
  5342.     *)
  5343.     myApplProp.tokenApplToken    := theApplToken;
  5344.     myApplProp.tokenApplProperty := theProperty;
  5345.     
  5346.   myErr := AECreateDesc(typeMyApplProp,
  5347.                                                 @myApplProp,
  5348.                                                 SizeOf(myApplProp),
  5349.                                                 value);
  5350.       
  5351.     IF (applDesc.dataHandle<>nil) THEN
  5352.         DisposHandle(Handle(applDesc.dataHandle));
  5353.         
  5354.     IF (propDesc.dataHandle<>nil) THEN
  5355.         DisposHandle(Handle(propDesc.dataHandle));
  5356.         
  5357.   PropertyFromApplAccessor := myErr;
  5358. END;    { PropertyFromApplAccessor }
  5359.  
  5360. FUNCTION MenuNameToMenuToken(theName:Str255;VAR theToken:MenuToken):OSErr;
  5361.     VAR
  5362.         index : INTEGER;
  5363.         
  5364.     BEGIN
  5365.         for index := appleM TO kLastMenu DO
  5366.           BEGIN
  5367.                 IF (IUEqualString(theName, myMenus[index]^^.menuData)=0) THEN
  5368.                   BEGIN
  5369.                         theToken.theTokenMenu := myMenus[index];
  5370.                         theToken.theTokenID   := index+appleID;
  5371.                         
  5372.                         MenuNameToMenuToken := noErr;
  5373.                         Exit(MenuNameToMenuToken);
  5374.                     END;
  5375.           END;
  5376.         MenuNameToMenuToken := errAEIllegalIndex;
  5377.     END;
  5378.  
  5379. FUNCTION MenuFromNullAccessor(wantClass     : DescType;
  5380.                                                             container     : AEDesc;
  5381.                                                             containerClass: DescType;
  5382.                                                             form          : DescType; 
  5383.                                                             selectionData : AEDesc;
  5384.                                                             VAR value     : AEDesc;
  5385.                                                             theRefCon     : LONGINT):OSErr;
  5386.     VAR    
  5387.         myErr      : OSErr;
  5388.         nameStr    : Str255;
  5389.         theMenu    : MenuToken;
  5390.         index      : INTEGER;
  5391.         resultDesc : AEDesc;
  5392.         
  5393.   BEGIN        
  5394.         myErr := errAEBadKeyForm;    (* or whatever *)
  5395.         
  5396.         value.dataHandle      := nil;
  5397.         resultDesc.dataHandle := nil;
  5398.         
  5399.         (* 
  5400.             should only be called with wantClass = cMenu and
  5401.             with containerClass = typeNull or typeMyAppl.
  5402.             Currently accept as either formName or formAbsolutePosition
  5403.         *)
  5404.         
  5405.         if ((wantClass <> cMenu) OR 
  5406.               ((containerClass <> typeNull) AND (containerClass <> typeMyAppl)) OR
  5407.                NOT((form = formName) OR (form = formAbsolutePosition))) THEN
  5408.             MenuFromNullAccessor:= errAEWrongDataType;
  5409.         
  5410.         IF (form = formName) THEN
  5411.             BEGIN
  5412.                 myErr := GetPStringFromDescriptor(selectionData, nameStr);
  5413.                 myErr := MenuNameToMenuToken(nameStr, theMenu);
  5414.             END;
  5415.         
  5416.         IF (form = formAbsolutePosition) THEN
  5417.             BEGIN
  5418.                 myErr     := GetIntegerFromDescriptor(selectionData, index);
  5419.                 IF (index<0) THEN
  5420.                     index := kLastMenu + index + 1;
  5421.                     
  5422.                 IF (index>0) AND (index<=kLastMenu+1) THEN
  5423.                     BEGIN
  5424.                         theMenu.theTokenMenu := myMenus[index-1];
  5425.                         theMenu.theTokenID   := index-1+appleID;
  5426.                     END
  5427.                 else
  5428.               myErr := errAEIllegalIndex;    (* or whatever *)
  5429.             END;
  5430.             
  5431.         IF (myErr = noErr) THEN
  5432.             myErr := AECreateDesc(typeMyMenu, Ptr(@theMenu), sizeof(theMenu), value);
  5433.                 
  5434.         MenuFromNullAccessor := myErr;
  5435.     END;    (* MenuFromNullAccessor *)
  5436.  
  5437. FUNCTION PropertyFromMenuAccessor(wantClass      : DescType;
  5438.                                                                     container      : AEDesc;
  5439.                                                                     containerClass : DescType;
  5440.                                                                     form           : DescType;
  5441.                                                                     selectionData  : AEDesc;
  5442.                                                                     VAR value      : AEDesc;
  5443.                                                                     theRefCon      : LONGINT) : OSErr;
  5444.   VAR
  5445.         myErr       : OSErr;
  5446.         ignoreErr   : OSErr;
  5447.         theMenuToken: MenuToken;
  5448.         theProperty : DescType;
  5449.         menuDesc    : AEDesc;
  5450.         propDesc    : AEDesc;
  5451.         actualSize  : Size;
  5452.         myMenuProp  : MenuPropToken;
  5453.             
  5454.     BEGIN
  5455.         value.dataHandle     := nil;
  5456.         menuDesc.dataHandle  := nil;
  5457.         propDesc.dataHandle  := nil;
  5458.         
  5459.         IF ((wantClass <> cProperty) OR
  5460.               (form <> formPropertyID)) THEN
  5461.             BEGIN
  5462.                 PropertyFromMenuAccessor := errAEWrongDataType;
  5463.                 Exit(PropertyFromMenuAccessor);
  5464.             END;
  5465.         
  5466.         (* get the menu token - it's the container *)
  5467.         
  5468.         myErr := AECoerceDesc(container, typeMyMenu, menuDesc);
  5469.         GetRawDataFromDescriptor(menuDesc,
  5470.                                                          Ptr(@theMenuToken),
  5471.                                                          sizeof(theMenuToken),
  5472.                                                          actualSize);
  5473.                 
  5474.         (* get the property - it's in the selection data *)
  5475.         
  5476.         myErr := AECoerceDesc(selectionData, typeType, propDesc);
  5477.         GetRawDataFromDescriptor(propDesc,
  5478.                                                          Ptr(@theProperty),
  5479.                                                          sizeof(theProperty),
  5480.                                                          actualSize);
  5481.         (*
  5482.             Combine the two into single token
  5483.         *)
  5484.         myMenuProp.theMenuToken := theMenuToken;
  5485.         myMenuProp.theMenuProp  := theProperty;
  5486.         
  5487.         myErr := AECreateDesc(typeMyMenuProp,
  5488.                                                   Ptr(@myMenuProp),
  5489.                                                   sizeof(myMenuProp),
  5490.                                                   value);
  5491.             
  5492.         IF (menuDesc.dataHandle<>nil) THEN
  5493.             ignoreErr := AEDisposeDesc(menuDesc);
  5494.             
  5495.         IF (propDesc.dataHandle<>nil) THEN
  5496.             ignoreErr := AEDisposeDesc(propDesc);
  5497.             
  5498.         PropertyFromMenuAccessor := myErr;
  5499. END;    (* PropertyFromMenuAccessor *)
  5500.  
  5501. FUNCTION PropertyFromMenuItemAccessor(wantClass             : DescType;
  5502.                                                                             container             : AEDesc;
  5503.                                                                             containerClass  : DescType;
  5504.                                                                             form            : DescType; 
  5505.                                                                             selectionData   :    AEDesc;
  5506.                                                                             VAR value       :    AEDesc;
  5507.                                                                             theRefCon       : longint):OSErr;
  5508.     VAR    
  5509.         myErr            : OSErr;
  5510.         ignoreErr        : OSErr;
  5511.         theMenuItemToken : MenuItemToken;
  5512.         theProperty      : DescType;
  5513.         itemDesc         : AEDesc;
  5514.         propDesc         : AEDesc;
  5515.         actualSize       : Size;
  5516.         myItemProp       : MenuItemPropToken;
  5517.             
  5518.     BEGIN
  5519.         value.dataHandle     := nil;
  5520.         itemDesc.dataHandle  := nil;
  5521.         propDesc.dataHandle  := nil;
  5522.         
  5523.         if ((wantClass <> cProperty) OR
  5524.               (form <> formPropertyID)) THEN
  5525.             BEGIN
  5526.                 PropertyFromMenuItemAccessor := errAEWrongDataType;
  5527.             END;
  5528.         
  5529.         (* get the menu token - it's the container *)
  5530.         
  5531.         myErr := AECoerceDesc(container, typeMyMenuItem, itemDesc);
  5532.         GetRawDataFromDescriptor(itemDesc,
  5533.                                                          Ptr(@theMenuItemToken),
  5534.                                                          sizeof(theMenuItemToken),
  5535.                                                          actualSize);
  5536.                 
  5537.         (* get the property - it's in the selection data *)
  5538.         
  5539.         myErr := AECoerceDesc(selectionData, typeType, propDesc);
  5540.         GetRawDataFromDescriptor(propDesc,
  5541.                                                          Ptr(@theProperty),
  5542.                                                          sizeof(theProperty),
  5543.                                                          actualSize);
  5544.         (*
  5545.             Combine the two into single token
  5546.         *)
  5547.         myItemProp.theItemToken  := theMenuItemToken;
  5548.         myItemProp.theItemProp   := theProperty;
  5549.         
  5550.         myErr := AECreateDesc(typeMyItemProp,
  5551.                                                   Ptr(@myItemProp),
  5552.                                                   sizeof(myItemProp),
  5553.                                                   value);
  5554.             
  5555.         IF (itemDesc.dataHandle<>NIL) THEN
  5556.             ignoreErr := AEDisposeDesc(itemDesc);
  5557.             
  5558.         IF (propDesc.dataHandle<>NIL) THEN
  5559.             ignoreErr := AEDisposeDesc(propDesc);
  5560.             
  5561.         PropertyFromMenuItemAccessor := myErr;
  5562. END;    (* PropertyFromMenuItemAccessor *)
  5563.  
  5564. FUNCTION ItemNameToItemIndex(theName:Str255; theMenu:MenuHandle; VAR theIndex: INTEGER):OSErr;
  5565.     VAR
  5566.         index    : INTEGER;
  5567.         maxItems : INTEGER;
  5568.         menuName : Str255;
  5569.         
  5570.     BEGIN
  5571.         maxItems := CountMItems(theMenu);
  5572.         
  5573.         FOR index := 1 TO maxItems DO
  5574.             BEGIN
  5575.                 GetItem(theMenu, index, menuName);
  5576.                 IF (IUEqualString(theName, menuName)=0) THEN
  5577.                     BEGIN
  5578.                         theIndex := index;
  5579.                         ItemNameToItemIndex := noErr;
  5580.                         Exit(ItemNameToItemIndex);
  5581.                     END;
  5582.             END;
  5583.         ItemNameToItemIndex := errAEIllegalIndex;
  5584.     END;
  5585.     
  5586. FUNCTION MenuItemFromMenuAccessor(wantClass             : DescType;
  5587.                                                                     container             : AEDesc;
  5588.                                                                     containerClass  : DescType;
  5589.                                                                     form            : DescType; 
  5590.                                                                     selectionData   :    AEDesc;
  5591.                                                                     VAR value       :    AEDesc;
  5592.                                                                     theRefCon       : longint) : OSErr;
  5593.   VAR
  5594.         myErr                 : OSErr;
  5595.         ignoreErr             : OSErr;
  5596.         theMenuItemToken : MenuItemToken;
  5597.         theMenuToken     : MenuToken;
  5598.         menuDesc         : AEDesc;
  5599.         actualSize       : Size;
  5600.         nameStr          : Str255;
  5601.         maxItems         : INTEGER;
  5602.         index            : INTEGER;
  5603.             
  5604.     BEGIN
  5605.         value.dataHandle     := nil;
  5606.         menuDesc.dataHandle  := nil;
  5607.         
  5608.         IF ((wantClass <> cMenuItem) OR
  5609.             (containerClass <> cMenu) OR
  5610.               ((form <> formAbsolutePosition) AND (form <> formName))) THEN
  5611.             BEGIN
  5612.                 MenuItemFromMenuAccessor := errAEWrongDataType;
  5613.                 Exit(MenuItemFromMenuAccessor);
  5614.             END;
  5615.         
  5616.         (* get the menu token - it's the container *)
  5617.         
  5618.         myErr := AECoerceDesc(container, typeMyMenu, menuDesc);
  5619.         GetRawDataFromDescriptor(menuDesc,
  5620.                                                          Ptr(@theMenuToken),
  5621.                                                          sizeof(theMenuToken),
  5622.                                                          actualSize);
  5623.                 
  5624.         IF (form=formAbsolutePosition) THEN
  5625.             BEGIN
  5626.                 myErr := GetIntegerFromDescriptor(selectionData, index);
  5627.                 maxItems := CountMItems(theMenuToken.theTokenMenu);
  5628.                 
  5629.                 IF (index<0) THEN
  5630.                     index := maxItems + index + 1;
  5631.                     
  5632.                 IF ((index<1) OR (index>maxItems)) THEN
  5633.                   myErr := errAEIllegalIndex;
  5634.             END;
  5635.             
  5636.         if (form = formName) THEN
  5637.             BEGIN
  5638.                 myErr  := GetPStringFromDescriptor(selectionData, nameStr);
  5639.                 myErr  := ItemNameToItemIndex(nameStr, theMenuToken.theTokenMenu, index);
  5640.             END;
  5641.         
  5642.         (*
  5643.             Combine the two into single token
  5644.         *)
  5645.     
  5646.         theMenuItemToken.theMenuToken  := theMenuToken;
  5647.         theMenuItemToken.theTokenItem  := index;
  5648.         
  5649.         IF (myErr=noErr) THEN
  5650.             myErr := AECreateDesc(typeMyMenuItem,
  5651.                                                       Ptr(@theMenuItemToken),
  5652.                                                       sizeof(theMenuItemToken),
  5653.                                                       value);
  5654.             
  5655.         IF (menuDesc.dataHandle<>NIL) THEN
  5656.             ignoreErr := AEDisposeDesc(menuDesc);
  5657.             
  5658.         MenuItemFromMenuAccessor := myErr;
  5659. END;    (* MenuItemFromMenuAccessor *)
  5660.         
  5661. {*****************************************************************************}
  5662. {
  5663.     Stuff for counting objects
  5664. }
  5665.  
  5666. FUNCTION MyCountProc(    desiredType        : DescType;
  5667.                                             containerClass: DescType;
  5668.                                             container            : AEDesc;
  5669.                                             VAR result        : LongInt): OSErr;
  5670.                                             
  5671. { so far all I count is:
  5672.   (1) the number of active windows in the app;
  5673.   (2) the number of words in a window 
  5674. }
  5675.  
  5676. VAR myErr                        :    OSErr;
  5677.         theWindowToken     : WindowToken;
  5678.         theDocument     : DPtr;
  5679.         theHTE          : TEHandle;
  5680.         newDesc                 : AEDesc;
  5681.         wordStart                : INTEGER;
  5682.         wordLength      : INTEGER;
  5683.         tokenSize                : Size;
  5684.         theTextToken        : TextToken;
  5685.         
  5686. BEGIN
  5687.   result := -1;    { easily recognized illegal value }
  5688.     
  5689.     myErr := errAEWrongDataType;
  5690.     
  5691.     IF (desiredType = cWindow) THEN
  5692.         BEGIN
  5693.             IF (containerClass = typeNull) OR
  5694.                  (containerClass = cApplication) THEN
  5695.                 result := CountWindows;
  5696.         END;
  5697.         
  5698.     IF (desiredType = cWord) OR
  5699.        (desiredType = cLine) OR
  5700.        (desiredType = cChar) THEN
  5701.         BEGIN
  5702.             myErr := AECoerceDesc(container, typeMyWndw, newDesc);
  5703.             IF (newDesc.descriptorType<>typeNull) THEN
  5704.                 BEGIN
  5705.                     GetRawDataFromDescriptor(newDesc,
  5706.                                                                      @theWindowToken,
  5707.                                                                      SizeOf(theWindowToken),
  5708.                                                                      tokenSize);
  5709.                                                                      
  5710.                     myErr := AEDisposeDesc(newDesc);
  5711.                                                                      
  5712.                     IF (theWindowToken=NIL) THEN
  5713.                         myErr := errAEIllegalIndex
  5714.                     ELSE
  5715.                         BEGIN
  5716.                     
  5717.                             theDocument := DPtrFromWindowPtr(theWindowToken);
  5718.                             theHTE      := theDocument^.theText;
  5719.                             
  5720.                             IF (desiredType = cWord) THEN
  5721.                                 BEGIN
  5722.                                     wordStart   := 0;
  5723.                                     wordLength  := theHTE^^.teLength;
  5724.                                     result      := CountWords(theHTE, wordStart, wordLength);
  5725.                                 END;
  5726.                             
  5727.                             IF (desiredType = cChar) THEN
  5728.                                 result := theHTE^^.teLength;
  5729.                                 
  5730.                             IF (desiredType = cLine) THEN
  5731.                                 result := CountLines(theHTE);
  5732.                                 
  5733.                         END;
  5734.                 END;
  5735.                 
  5736.             myErr := AECoerceDesc(container, typeMyText, newDesc);
  5737.             IF (newDesc.descriptorType<>typeNull) THEN
  5738.                 BEGIN
  5739.                     GetRawDataFromDescriptor(    newDesc,
  5740.                                                                         @theTextToken,
  5741.                                                                         sizeOf(theTextToken),
  5742.                                                                         tokenSize);
  5743.                                                                      
  5744.                     myErr := AEDisposeDesc(newDesc);
  5745.                                                                      
  5746.                     theDocument := DPtrFromWindowPtr(theTextToken.tokenWindow);
  5747.                     theHTE      := theDocument^.theText;
  5748.                     
  5749.                     IF (desiredType = cWord) THEN
  5750.                         BEGIN
  5751.                             wordStart   := theTextToken.tokenOffset-1;
  5752.                             wordLength  := theTextToken.tokenLength;
  5753.                             result      := CountWords(theHTE, wordStart, wordLength);
  5754.                         END;
  5755.                     
  5756.                    IF (desiredType = cChar) THEN
  5757.                         result := theTextToken.tokenLength;
  5758.                         
  5759.                     IF (desiredType = cLine) THEN
  5760.                         result:= LineOfOffset(theHTE,theTextToken.tokenOffset-1) - 
  5761.                                  LineOfOffset(theHTE,theTextToken.tokenOffset+theTextToken.tokenLength-1)
  5762.                                          +1;                        
  5763.                 END;
  5764.                 
  5765.         END;
  5766.         
  5767.     MyCountProc := myErr;
  5768. END;    { MyCountProc }
  5769.  
  5770. {*****************************************************************************}
  5771. {
  5772.     Coercion Handlers - Allow AEResolve to do the hard work
  5773. }
  5774. FUNCTION CoerceObjToAnything(theAEDesc         : AEDesc;
  5775.                                                          toType            : DescType;
  5776.                                                      handlerRefCon: LongInt;
  5777.                                                          VAR result        :  AEDesc): OSErr;
  5778. {
  5779.     CoerceObjToAnything functions by using AEResolve to do the hard
  5780.     work.
  5781. }
  5782. CONST kAEIDoMinimum = 0;
  5783.  
  5784. VAR     myErr            : OSErr;
  5785.             objDesc        : AEDesc;
  5786.             
  5787. BEGIN
  5788.   myErr := errAECoercionFail;
  5789.     
  5790.     result.dataHandle := NIL;
  5791.     objDesc.dataHandle := NIL;
  5792.     
  5793.   
  5794.   IF (theAEDesc.descriptorType <> typeObjectSpecifier) THEN
  5795.     BEGIN
  5796.             CoerceObjToAnything := errAEWrongDataType;
  5797.             Exit(CoerceObjToAnything);
  5798.         END;
  5799.     
  5800.   { resolve the object specifier }
  5801.   myErr := AEResolve(theAEDesc,kAEIDoMinimum,objDesc);
  5802.       
  5803.   { hopefully it's the right type by now, but we'll give it a nudge }
  5804.   IF (myErr=noErr) THEN
  5805.         BEGIN
  5806.             myErr := AECoerceDesc(objDesc,toType,result);
  5807.            myErr := AEDisposeDesc(objDesc);
  5808.         END;
  5809.         
  5810.     IF (result.descriptorType<>toType) THEN
  5811.         BEGIN
  5812.             {DebugStr('COTA - Not of requested type');}
  5813.         END;
  5814.       
  5815.   CoerceObjToAnything := myErr;
  5816. END;    { CoerceObjToAnything }
  5817.  
  5818. {*****************************************************************************}
  5819.  
  5820. {----------------------------------------------------------------------------------------------}
  5821.  
  5822. {now for the edition manager event handling code}
  5823.  
  5824. FUNCTION GetHandleFromEvent(theAppleEvent : AppleEvent; VAR sectionH : SectionHandle):OSErr;
  5825.     
  5826.     VAR ignoreType : DescType;
  5827.             ignoreSize : Size;
  5828.     
  5829.     BEGIN
  5830.         GetHandleFromEvent := AEGetKeyPtr(theAppleEvent, keyDirectObject, typeSectionH, 
  5831.                                                                             ignoreType, @sectionH, SizeOf(sectionHandle), ignoreSize);
  5832.     END;
  5833.  
  5834. {----------------------------------------------------------------------------------------------}
  5835. FUNCTION DoReadSection(theAppleEvent, reply : AppleEvent; refCon : LONGINT): OSErr;
  5836.     
  5837.     VAR err          : OSErr;
  5838.             sectionH : SectionHandle;
  5839.     
  5840.     BEGIN
  5841.         err := GetHandleFromEvent(theAppleEvent, sectionH);
  5842.         IF IsRegisteredSection(sectionH) = noErr THEN
  5843.             ReadAnEdition(sectionH);
  5844.         DoReadSection := err;
  5845.     END;
  5846.     
  5847. {----------------------------------------------------------------------------------------------}
  5848. FUNCTION DoWriteSection(theAppleEvent, reply : AppleEvent; refCon : LONGINT): OSErr;
  5849.     
  5850.     VAR err          : OSErr;
  5851.             sectionH : SectionHandle;
  5852.     
  5853.     BEGIN
  5854.         err := GetHandleFromEvent(theAppleEvent, sectionH);
  5855.         IF IsRegisteredSection(sectionH) = noErr THEN
  5856.             WriteAnEdition(sectionH);
  5857.         DoWriteSection := err;
  5858.     
  5859.     END;
  5860.  
  5861. {----------------------------------------------------------------------------------------------}
  5862. FUNCTION DoScrollSection(theAppleEvent, reply : AppleEvent; refCon : LONGINT): OSErr;
  5863.     
  5864.     VAR err                 : OSErr;
  5865.             sectionH        : SectionHandle;
  5866.             aSectHandle : SectHandle;
  5867.     
  5868.     BEGIN
  5869.         err := GetHandleFromEvent(theAppleEvent, sectionH);
  5870.         {get at the sectHandle}
  5871.         aSectHandle := SectHandle(GetERefCon(sectionH));
  5872.         TESetSelect(aSectHandle^^.fStart, aSectHandle^^.fEnd, aSectHandle^^.fDocument^.theText);
  5873.         ShowSelect(aSectHandle^^.fDocument);
  5874.         DoScrollSection := err;
  5875.     END;
  5876.  
  5877. {----------------------------------------------------------------------------------------------}
  5878. FUNCTION DoCancelSection(theAppleEvent, reply : AppleEvent; refCon : LONGINT): OSErr;
  5879.  
  5880.     VAR err                    : OSErr;
  5881.             sectionH        : SectionHandle;
  5882.             aSectHandle : SectHandle;
  5883.             
  5884.     BEGIN
  5885.         err := GetHandleFromEvent(theAppleEvent, sectionH);
  5886.         aSectHandle := SectHandle(GetERefCon(sectionH));
  5887.         err := UnRegisterSection(sectionH);
  5888.         DeleteASection(aSectHandle, aSectHandle^^.fDocument);
  5889.         DoCancelSection := noErr;
  5890.     END;
  5891.  
  5892. { -----------------------------------------------------------------------
  5893.         Name:             InitAppleEvents
  5894.         Purpose:        Initialise the AppleEvent despatch table
  5895.      -----------------------------------------------------------------------*}
  5896.  
  5897. {$S Main}
  5898. PROCEDURE InitAppleEvents;
  5899.  
  5900. CONST noRefCon = -1;
  5901. VAR
  5902.         aevtErr: OSErr ;
  5903. BEGIN
  5904.     gBigBrother := 0;
  5905.         gCharsInBuffer := 0;
  5906.         gTypingBuffer  := typingBufPtr(NewPtr(32000));
  5907.         gTypingTargetObject.dataHandle := NIL;
  5908.  
  5909.         
  5910.         {set up the despatch table for the four standard apple events}
  5911.         
  5912.         aevtErr := AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, @DoOpenApp, noRefCon, FALSE) ;
  5913.         aevtErr := AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,   @DoOpenDocument, noRefCon, FALSE) ;
  5914.         aevtErr := AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,  @DoPrintDocuments, noRefCon, FALSE) ;
  5915.         aevtErr := AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, @MyQuit, noRefCon, FALSE) ;
  5916.         
  5917.         { set up the despatch table for the core AppleEvents for text }
  5918.         
  5919.         aevtErr := AEInstallEventHandler( kAECoreSuite,     kAEDelete, @DoDeleteEdit,noRefCon, FALSE);
  5920.  
  5921.         aevtErr := AEInstallEventHandler( kAEMiscStandards, kAECut,    @DoCutEdit,   noRefCon, FALSE);
  5922.         aevtErr := AEInstallEventHandler( kAEMiscStandards, kAECopy,   @DoCopyEdit,  noRefCon, FALSE);
  5923.         aevtErr := AEInstallEventHandler( kAEMiscStandards, kAEPaste,  @DoPasteEdit, noRefCon, FALSE);
  5924.         aevtErr := AEInstallEventHandler( kAECoreSuite,     kAESetData,@DoSetData,   noRefCon, FALSE);
  5925.         aevtErr := AEInstallEventHandler( kAECoreSuite,     kAEGetData,@DoGetData,   noRefCon, FALSE);
  5926.         aevtErr := AEInstallEventHandler( kAECoreSuite,     kAEGetDataSize,@DoGetDataSize,   noRefCon, FALSE);
  5927.         
  5928.         aevtErr := AEInstallEventHandler( kAECoreSuite, kAECountElements,   @HandleNumberOfElements,   noRefCon, FALSE);
  5929.         aevtErr := AEInstallEventHandler( kAECoreSuite, kAECreateElement,   @DoNewElement,   noRefCon, FALSE);
  5930.         aevtErr := AEInstallEventHandler( kAECoreSuite, kAEDoObjectsExist,  @DoIsThereA,   noRefCon, FALSE);
  5931.     
  5932.         aevtErr := AEInstallEventHandler( kAECoreSuite,     kAEClose,  @DoCloseWindow,noRefCon, FALSE);
  5933.         aevtErr := AEInstallEventHandler( kAECoreSuite,     kAESave,   @DoSaveWindow,noRefCon, FALSE);
  5934.         aevtErr := AEInstallEventHandler( kAEMiscStandards, kAERevert, @DoRevertWindow,noRefCon, FALSE);
  5935.         
  5936.         aevtErr := AEInstallEventHandler( kAEMiscStandards, kAECreatePublisher,    @HandleCreatePub, noRefCon, FALSE);
  5937.         aevtErr := AEInstallEventHandler( kAEMiscStandards, kAEMakeObjectsVisible, @HandleShowSelection,   noRefCon, FALSE);
  5938.         
  5939.         { Now look for recording notifications }
  5940.         
  5941.         aevtErr := AEInstallEventHandler( kCoreEventClass, kAEStartedRecording, @HandleStartRecording, noRefCon, FALSE);
  5942.         aevtErr := AEInstallEventHandler( kCoreEventClass, kAEStoppedRecording, @HandleStopRecording, noRefCon, FALSE);
  5943.         
  5944.         { Now Put in the required object accessors }
  5945.         
  5946.         aevtErr := AESetObjectCallbacks(NIL,@MyCountProc,NIL,NIL,NIL,NIL,NIL);
  5947.         
  5948.         aevtErr := AEInstallObjectAccessor(cWindow,      typeNull,   @WindowFromNullAccessor,  0,FALSE);
  5949.         aevtErr := AEInstallObjectAccessor(cWindow,      typeMyAppl, @WindowFromNullAccessor,  0,FALSE);
  5950.         aevtErr := AEInstallObjectAccessor(cApplication, typeNull,   @ApplicationFromNullAccessor,  0,FALSE);
  5951.         aevtErr := AEInstallObjectAccessor(cProperty,    typeMyAppl, @PropertyFromApplAccessor,0,FALSE);
  5952.         
  5953.         aevtErr := AEInstallObjectAccessor(cChar,             typeMyWndw,@TextElemFromWndwAccessor,0,FALSE);
  5954.         aevtErr := AEInstallObjectAccessor(cSpot,             typeMyWndw,@TextElemFromWndwAccessor,0,FALSE);
  5955.         aevtErr := AEInstallObjectAccessor(cWord,             typeMyWndw,@TextElemFromWndwAccessor,0,FALSE);
  5956.         aevtErr := AEInstallObjectAccessor(cLine,             typeMyWndw,@TextElemFromWndwAccessor,0,FALSE);
  5957.         aevtErr := AEInstallObjectAccessor(cSelection, typeMyWndw,@TextElemFromWndwAccessor,0,FALSE);
  5958.         aevtErr := AEInstallObjectAccessor(cProperty,  typeMyWndw,@PropertyFromWndwAccessor,0,FALSE);
  5959.     
  5960.         aevtErr := AEInstallObjectAccessor(cProperty,     typeMyText,@PropertyFromTextAccessor,0,FALSE);
  5961.         aevtErr := AEInstallObjectAccessor(cChar,           typeMyText,@TextElemFromTextAccessor,0,FALSE);
  5962.         aevtErr := AEInstallObjectAccessor(cWord,           typeMyText,@TextElemFromTextAccessor,0,FALSE);
  5963.         aevtErr := AEInstallObjectAccessor(cLine,           typeMyText,@TextElemFromTextAccessor,0,FALSE);
  5964.         aevtErr := AEInstallObjectAccessor(cSpot,           typeMyText,@TextElemFromTextAccessor,0,FALSE);
  5965.         
  5966.         aevtErr := AEInstallObjectAccessor(cMenu,            typeNull,       @MenuFromNullAccessor,    0,false);
  5967.         aevtErr := AEInstallObjectAccessor(cProperty,   typeMyMenu,     @PropertyFromMenuAccessor,0,false);
  5968.         aevtErr := AEInstallObjectAccessor(cProperty,   typeMyMenuItem, @PropertyFromMenuItemAccessor,0,false);
  5969.         aevtErr := AEInstallObjectAccessor(cMenuItem,   typeMyMenu,     @MenuItemFromMenuAccessor,0,false);
  5970.         { Now the coercion handlers }
  5971.  
  5972.       aevtErr := AEInstallCoercionHandler(typeObjectSpecifier,typeMyAppl,      @CoerceObjToAnything,0,TRUE,FALSE);
  5973.         aevtErr := AEInstallCoercionHandler(typeObjectSpecifier,typeMyWndw,      @CoerceObjToAnything,0,TRUE,FALSE);
  5974.       aevtErr := AEInstallCoercionHandler(typeObjectSpecifier,typeMyText,      @CoerceObjToAnything,0,TRUE,FALSE);
  5975.       aevtErr := AEInstallCoercionHandler(typeObjectSpecifier,typeMyTextProp,  @CoerceObjToAnything,0,TRUE,FALSE);
  5976.       aevtErr := AEInstallCoercionHandler(typeObjectSpecifier,typeMyWindowProp,@CoerceObjToAnything,0,TRUE,FALSE);
  5977.       aevtErr := AEInstallCoercionHandler(typeObjectSpecifier,typeMyApplProp,  @CoerceObjToAnything,0,TRUE,FALSE);
  5978.       aevtErr := AEInstallCoercionHandler(typeObjectSpecifier,typeMyMenu,      @CoerceObjToAnything,0,true,false);
  5979.       aevtErr := AEInstallCoercionHandler(typeObjectSpecifier,typeMyMenuProp,  @CoerceObjToAnything,0,true,false);
  5980.       aevtErr := AEInstallCoercionHandler(typeObjectSpecifier,typeMyMenuItem,  @CoerceObjToAnything,0,true,false);
  5981.       aevtErr := AEInstallCoercionHandler(typeObjectSpecifier,typeMyItemProp,  @CoerceObjToAnything,0,true,false);
  5982.         {now install the appropriate edition manager events}
  5983.         
  5984.         aevtErr := AEInstallEventHandler( sectionEventmsgClass, sectionReadMsgID, @DoReadSection, noRefCon, FALSE) ;
  5985.         aevtErr := AEInstallEventHandler( sectionEventmsgClass, sectionWriteMsgID, @DoWriteSection, noRefCon, FALSE) ;
  5986.         aevtErr := AEInstallEventHandler( sectionEventmsgClass, sectionScrollMsgID, @DoScrollSection, noRefCon, FALSE) ;
  5987.         aevtErr := AEInstallEventHandler( sectionEventmsgClass, sectionCancelMsgID, @DoCancelSection, noRefCon, FALSE) ;
  5988.         
  5989. END;
  5990.  
  5991.         
  5992. END.
  5993.